Skip to main content

Module forms

Module forms 

Source
Expand description

Form parsing, validation, and saving — shared between the auto-admin and user route handlers.

§Three form types

TypeWhen to use
Form + #[derive(Form)]Typed struct with declared fields and compile-time validators
ModelFormAny [Model] table — parse + validate + save without a dedicated struct
DynamicFormRuntime 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§

DynamicField
One field descriptor in a DynamicForm.
DynamicForm
Runtime JSON-schema driven form.
FormErrors
All validation errors collected from a form submission.
ModelForm
Schema-driven form that can insert or update any [Model] row.
ModelFormFor
Runtime ModelForm — parse a string-keyed payload into a typed (columns, values) pair against a T: Model schema, ready to feed straight into sql::insert / sql::update / their _pool variants.

Enums§

DynamicFieldType
Field types supported in a DynamicForm schema.
FormError
Single-field error type used by the low-level parsers and the admin.
ModelFormError
Error returned by ModelForm::save.

Traits§

Form
Trait every #[derive(Form)] struct implements.
FormStructDeprecated
Backwards-compatible alias — prefer Form.

Functions§

collect_values
Walk every scalar field of model and turn the form payload into a (column, value) list ready to feed an InsertQuery / UpdateQuery. skip is 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.