Skip to main content

FieldType

Trait FieldType 

Source
pub trait FieldType:
    Send
    + Sync
    + 'static {
    // Required methods
    fn view_key(&self) -> &'static str;
    fn view_model(&self, cx: &FieldCx<'_>) -> FieldVm;
    fn render_default(&self, vm: &FieldVm) -> Markup;

    // Provided methods
    fn resolve_options(
        &self,
        _raw: &Value,
        _types: &FieldRegistry,
    ) -> Result<ResolvedOptions, OptionsError> { ... }
    fn option_keys(&self, _raw: &Value) -> Option<Vec<String>> { ... }
    fn intrinsic_rules(&self, _opts: &ResolvedOptions) -> Vec<Rule> { ... }
    fn to_attr(
        &self,
        field: &SubmittedField<'_>,
        opts: &ResolvedOptions,
        mode: Mode,
    ) -> Result<Option<AttrValue>, String> { ... }
    fn to_control(&self, stored: Option<&str>, opts: &ResolvedOptions) -> String { ... }
    fn submitted(
        &self,
        field: &SubmittedField<'_>,
        opts: &ResolvedOptions,
    ) -> Option<String> { ... }
    fn chrome(&self) -> Chrome { ... }
    fn assets(&self, _opts: &ResolvedOptions) -> Vec<&'static str> { ... }
}

Required Methods§

Source

fn view_key(&self) -> &'static str

The stable key for override resolution and default-template selection (bare like text for core; dotted vendor.name for plugins).

Source

fn view_model(&self, cx: &FieldCx<'_>) -> FieldVm

Builds the serialisable presentation payload. Pure, no IO.

Source

fn render_default(&self, vm: &FieldVm) -> Markup

The built-in presenter over the view-model (an Askama partial). Not called when an override wins.

Provided Methods§

Source

fn resolve_options( &self, _raw: &Value, _types: &FieldRegistry, ) -> Result<ResolvedOptions, OptionsError>

Types the descriptor’s raw options blob once at boot. A failure aborts boot naming the resource + field. Default: no options.

types is the field registry, for a composite field (a repeater) that holds fields of its own and has to resolve their types and options here, while the registry is in hand.

Source

fn option_keys(&self, _raw: &Value) -> Option<Vec<String>>

The keys this type reads from a field’s entry, beyond the ones every field has (rows for a textarea, options for a select). A key outside the list is refused when the form is prepared, naming the field and the keys accepted, so a typo never passes silently. None accepts any key unchecked; declare yours.

Source

fn intrinsic_rules(&self, _opts: &ResolvedOptions) -> Vec<Rule>

Rules this type always contributes, merged before the descriptor’s own (e.g. the text field’s email input contributes Rule::Email).

Source

fn to_attr( &self, field: &SubmittedField<'_>, opts: &ResolvedOptions, mode: Mode, ) -> Result<Option<AttrValue>, String>

How a submitted value becomes the attribute that gets stored.

raw is None when the submission omits the field entirely, which is how an unchecked checkbox arrives. Returning None leaves the attribute out of the write, so the column keeps whatever it held: that is how a blank password on an edit means “unchanged” rather than “erase”.

Validation has already run, so a type that needs its input to parse should guarantee that with an intrinsic_rules entry and treat the value here as sound. The error is for a genuine failure (hashing), not a bad value, and surfaces as a failed save.

Default: the submitted text, unchanged.

Source

fn to_control(&self, stored: Option<&str>, opts: &ResolvedOptions) -> String

How a stored value is presented back in the control on an edit.

The counterpart of to_attr: storage and presentation differ whenever a type stores something other than the text the browser sent, and a type that never shows its value (a password) returns the empty string here.

Default: the stored text, unchanged.

Source

fn submitted( &self, field: &SubmittedField<'_>, opts: &ResolvedOptions, ) -> Option<String>

What a field of several controls submitted, as the one value its control is rebuilt from.

A form holds one value a field, and a checklist submits a key for every box ticked and none under its own name. Gathered here into one value, the rules see the field, so required means a box is ticked, and a refused save comes back with the operator’s ticks in place. None when the submission holds nothing of this field.

Default: the value under the field’s own name.

Source

fn chrome(&self) -> Chrome

Whether the framework wraps this field in standard chrome.

Source

fn assets(&self, _opts: &ResolvedOptions) -> Vec<&'static str>

Asset-registry keys this type’s control needs on any page that renders it (a heavy widget’s script/style). The page shell collects, dedupes, and emits them; keys must exist in the asset registry. Default: none, so a type whose widget ships in core laterite.js declares nothing.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§