Skip to main content

Form

Derive Macro Form 

Source
#[derive(Form)]
{
    // Attributes available to this derive:
    #[form]
}
Expand description

Derive rustango::forms::FormStruct (slice 8.4B). Generates a parse(&HashMap<String, String>) -> Result<Self, FormError> impl that walks every named field and:

  • Parses the string value into the field’s Rust type (String, i32, i64, f32, f64, bool, plus Option<T> for the nullable case).
  • Applies any #[form(min = ..)] / #[form(max = ..)] / #[form(min_length = ..)] / #[form(max_length = ..)] validators in declaration order, returning FormError::Parse on the first failure.

Example:

#[derive(Form)]
pub struct CreateItemForm {
    #[form(min_length = 1, max_length = 64)]
    pub name: String,
    #[form(min = 0, max = 150)]
    pub age: i32,
    pub active: bool,
    pub email: Option<String>,
}

let parsed = CreateItemForm::parse(&form_map)?;