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§
Sourcefn fields(&self) -> Box<dyn DoubleEndedIterator<Item = &dyn DynFormField> + '_>
fn fields(&self) -> Box<dyn DoubleEndedIterator<Item = &dyn DynFormField> + '_>
Returns an iterator over the fields in the form.
Sourcefn set_value(
&mut self,
field_id: &str,
value: Cow<'_, str>,
) -> Result<(), FormFieldValidationError>
fn set_value( &mut self, field_id: &str, value: Cow<'_, str>, ) -> Result<(), FormFieldValidationError>
Sourcefn errors_for(&self, target: FormErrorTarget<'_>) -> &[FormFieldValidationError]
fn errors_for(&self, target: FormErrorTarget<'_>) -> &[FormFieldValidationError]
Returns the validation errors for a target in the form context.
Sourcefn errors_for_mut(
&mut self,
target: FormErrorTarget<'_>,
) -> &mut Vec<FormFieldValidationError>
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.
Sourcefn has_errors(&self) -> bool
fn has_errors(&self) -> bool
Returns whether the form context has any validation errors.
Provided Methods§
Sourcefn add_error(
&mut self,
target: FormErrorTarget<'_>,
error: FormFieldValidationError,
)
fn add_error( &mut self, target: FormErrorTarget<'_>, error: FormFieldValidationError, )
Adds a validation error to the form context.