Trait FormContext

Source
pub trait FormContext: Debug {
    // Required methods
    fn new() -> Self
       where Self: Sized;
    fn fields(
        &self,
    ) -> Box<dyn DoubleEndedIterator<Item = &dyn DynFormField> + '_>;
    fn set_value(
        &mut self,
        field_id: &str,
        value: Cow<'_, str>,
    ) -> Result<(), FormFieldValidationError>;
    fn errors_for(
        &self,
        target: FormErrorTarget<'_>,
    ) -> &[FormFieldValidationError];
    fn errors_for_mut(
        &mut self,
        target: FormErrorTarget<'_>,
    ) -> &mut Vec<FormFieldValidationError>;
    fn has_errors(&self) -> bool;

    // Provided method
    fn add_error(
        &mut self,
        target: FormErrorTarget<'_>,
        error: FormFieldValidationError,
    ) { ... }
}
Expand description

A trait for form contexts.

A form context is used to store the state of a form, such as the values of the fields and any errors that occur during validation. This trait is used to define the interface for a form context, which is used to interact with the form fields and errors.

This trait is typically not implemented directly; instead, its implementations are generated automatically through the Form derive macro.

Required Methods§

Source

fn new() -> Self
where Self: Sized,

Creates a new form context without any initial form data.

Source

fn fields(&self) -> Box<dyn DoubleEndedIterator<Item = &dyn DynFormField> + '_>

Returns an iterator over the fields in the form.

Source

fn set_value( &mut self, field_id: &str, value: Cow<'_, str>, ) -> Result<(), FormFieldValidationError>

Sets the value of a form field.

§Errors

This method should return an error if the value is invalid.

Source

fn errors_for(&self, target: FormErrorTarget<'_>) -> &[FormFieldValidationError]

Returns the validation errors for a target in the form context.

Source

fn errors_for_mut( &mut self, target: FormErrorTarget<'_>, ) -> &mut Vec<FormFieldValidationError>

Returns a mutable reference to the validation errors for a target in the form context.

Source

fn has_errors(&self) -> bool

Returns whether the form context has any validation errors.

Provided Methods§

Source

fn add_error( &mut self, target: FormErrorTarget<'_>, error: FormFieldValidationError, )

Adds a validation error to the form context.

Implementors§