Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Writing a Field Type

A field type renders one kind of control on any form or settings page that names it, and turns what it submits into the value that reaches the database.

Implement the type

impl FieldType for SwitchField {
    fn view_key(&self) -> &'static str {
        "switch"
    }

    fn view_model(&self, cx: &FieldCx<'_>) -> FieldVm {
        scalar_vm("switch", cx)
    }

    fn render_default(&self, vm: &FieldVm) -> Markup {
        Markup::from_template(&SwitchTmpl { vm }).unwrap_or_default()
    }

    fn to_attr(
        &self,
        field: &SubmittedField<'_>,
        _opts: &ResolvedOptions,
        _mode: Mode,
    ) -> Result<Option<AttrValue>, String> {
        Ok(Some(AttrValue::Bool(stored_is_on(field.value()))))
    }
}
MethodRole
view_keyThe name a descriptor uses. A dotted vendor.name; a taken key aborts the boot.
view_modelThe submitted or saved value as a view-model.
render_defaultThe markup.
to_attrThe submitted string as the typed value that reaches the database. Err refuses the save.
option_keysThe keys the type reads from a field’s entry. Any other key is refused by name.
submittedFor a type of several controls: what it submitted, gathered into one value. The rules read it, and a refused save is rebuilt from it.
resolve_optionsThose keys, typed once.

Built in: text, textarea, select, radio, checklist, taglist, switch, date, password, reference, repeater.

Register it

registry.add_field_type(FieldTypeReg::new(Arc::new(MoneyField)));

FormField::of("price", "Price", "acme.money")

One registration covers forms and settings. A descriptor naming an unregistered type aborts the boot.

Declare its keys

price: { type: acme.money, currency: EUR, precision: 2, span: 1/3 }
fn option_keys(&self, _raw: &serde_json::Value) -> Option<Vec<String>> {
    Some(vec!["currency".into(), "precision".into()])
}

A type’s keys are written on the field, beside the field’s own.

Meet the contract

RuleHow
Works with no configurationEvery key has a default chosen for the common case: type: acme.money alone renders a usable control.
Each default can be turned offOne key per behaviour, named for what it does, read from the field’s entry.
Its keys are declaredoption_keys lists them; a typo is refused by name.
Enter stays inside itA root marked data-lat-enter-scope when the control holds an input of its own. See JavaScript API.
It says what it didlat.emit(root, 'acme.money:changed', detail); a before- event is cancelable.
It can be drivenThe island’s initialiser returns its controller; lat.get(el) finds it.
It is listedOne row per behaviour in your plugin’s docs: the default, and the key that changes it. See Behaviours.
Works before scripts loadThe markup submits as a plain form; a script island improves it.