Skip to main content

RecordForm

Derive Macro RecordForm 

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

Derive RecordForm for the typed value a resource’s form writes.

One field per model column the form writes, named and typed like the model’s field. A scalar (String, a TypedValue type, or an Option of one) binds the key its control posts; a #[form(embed)] field binds every key of an EmbeddedForm value and is written whole.

#[derive(tablo_core::RecordForm)]
#[form(model = User)]
pub struct UserForm {
    pub name: String,
    #[form(blank = "member")]
    pub role: String,
    #[form(blank = 0)]
    pub age: i64,
}

The derive also emits UserFormField, one variant per field, which Posted keys on and RecordForm::fields answers with each variant’s keys. It emits UserFormControls, one control per field chosen from the field — a bool is a toggle, #[form(options = T)] a choice over T’s options, #[form(choice)] a bare choice, #[form(file)] a file field, #[form(embed)] the embedded value’s schema, and any other field a text field — with controls() handing them over and RecordForm::schema arranging one per field in declaration order. RecordForm::table lists a sortable column per text field, searchable over a String or Option<String>, an options field by its option’s label, and a toggle as yes or no. A resource’s ResourceDef defaults its form and table to them; ResourceDef::form and ResourceDef::table arrange or extend them instead.

§Attributes

  • #[form(model = User)] on the struct: the model the form writes.
  • #[form(blank = <expr>)] on a scalar: the value an empty submission reads as, overriding the default (String answers "" and Option<T> answers None through the type’s own blank, and bool answers false through the derive’s default).
  • #[form(options = Status)]: a choice over Status::options().
  • #[form(choice)]: a bare choice, whose options or relationship the resource’s form may add.
  • #[form(file)] on a String: a file field.
  • #[form(embed)] on an EmbeddedForm value.

A generic struct, a tuple struct, an empty struct, a Deferred<_> field, blank on an Option or an embedded value, and an unknown key are compile errors. So are a field the model lacks, a type the model’s field does not have, and a scalar that is not a FormScalar. Derive RecordForm for the typed value a resource’s form writes; documented on the tablo-core re-export.