Expand description
Form parsing, validation, and saving — shared between the auto-admin and user route handlers.
§Three form types
| Type | When to use |
|---|---|
Form + #[derive(Form)] | Typed struct with declared fields and compile-time validators |
ModelForm | Any [Model] table — parse + validate + save without a dedicated struct |
DynamicForm | Runtime JSON-schema forms (surveys, intake, admin-configurable) |
§#[derive(Form)] usage
ⓘ
use rustango::forms::Form;
#[derive(Form)]
struct ContactForm {
#[form(max_length = 100)]
name: String,
#[form(max_length = 200)]
email: String,
#[form(required = false)]
message: Option<String>,
}
// In a handler:
let form_data: HashMap<String, String> = /* axum Form extractor */;
match ContactForm::parse(&form_data) {
Ok(form) => { /* use form.name, form.email, form.message */ }
Err(errors) => { /* render errors */ }
}§ModelForm usage
ⓘ
use rustango::forms::ModelForm;
// Insert
let form = ModelForm::new(Post::SCHEMA, form_data);
match form.save(&pool).await {
Ok(pk) => redirect(pk),
Err(ModelFormError::Validation(errors)) => render_with_errors(errors),
Err(ModelFormError::Database(e)) => server_error(e),
}
// Update (provide the existing PK)
let pk = SqlValue::I64(post_id);
let form = ModelForm::for_update(Post::SCHEMA, form_data, pk);
form.save(&pool).await?;Modules§
- csrf
- CSRF middleware (slice 8.4C) — double-submit-cookie strategy.
Structs§
- Dynamic
Field - One field descriptor in a
DynamicForm. - Dynamic
Form - Runtime JSON-schema driven form.
- Form
Errors - All validation errors collected from a form submission.
- Model
Form - Schema-driven form that can insert or update any [
Model] row. - Model
Form For - Runtime ModelForm — parse a string-keyed payload into a typed
(columns, values)pair against aT: Modelschema, ready to feed straight intosql::insert/sql::update/ their_poolvariants.
Enums§
- Dynamic
Field Type - Field types supported in a
DynamicFormschema. - Form
Error - Single-field error type used by the low-level parsers and the admin.
- Model
Form Error - Error returned by
ModelForm::save.
Traits§
- Form
- Trait every
#[derive(Form)]struct implements. - Form
Struct Deprecated - Backwards-compatible alias — prefer
Form.
Functions§
- collect_
values - Walk every scalar field of
modeland turn the form payload into a(column, value)list ready to feed anInsertQuery/UpdateQuery.skipis a list of field names to omit. - parse_
form_ value - Parse one form value from a raw string.
- parse_
pk_ string - Parse a single PK fragment from a URL path segment into an
SqlValue.