pub struct Field<'a> { /* private fields */ }
Expand description
Represents a form field/element.
A Field
can convert itself to JSON (and other formats, using
serde
) and can perform validation of its Value
s based on
its Constraint
s. Note that the value
field always contains a Vec
of Value
s, close to how HTML url encoding works (where every key can
appear more than once and every value is a string, or optional).
Implementations§
Source§impl<'a> Field<'a>
impl<'a> Field<'a>
Sourcepub fn new(
name: &'a str,
label: &'a str,
element: Element,
required: bool,
values: Option<Vec<Value>>,
choices: &'a [(&'a str, &'a str)],
constraints: Vec<Constraint<'a>>,
attributes: Vec<Attr<'a>>,
) -> Field<'a>
pub fn new( name: &'a str, label: &'a str, element: Element, required: bool, values: Option<Vec<Value>>, choices: &'a [(&'a str, &'a str)], constraints: Vec<Constraint<'a>>, attributes: Vec<Attr<'a>>, ) -> Field<'a>
Initialize a Field
.
It is advised to use the builder-style methods on HtmlForm to instantiate, since those perform additional checks and prepare arguments.
Sourcepub fn values(&self) -> Vec<Value>
pub fn values(&self) -> Vec<Value>
Returns a Vec
of 0 or more non-empty values from self.values.
Sourcepub fn validate(&self, values: &[&Value]) -> Result<(), ValidationError>
pub fn validate(&self, values: &[&Value]) -> Result<(), ValidationError>
Validate a set of values
Note that this assumes values
contains the correct amount of
non-empty values for this Field
, so dealing with errors
regarding self.required
and amount of values should be done before
this method is called.
Generally, this method is not called directly, but indirectly by
HtmlForm
’s update()
.
Sourcepub fn set_values(&mut self, values: Vec<&Value>)
pub fn set_values(&mut self, values: Vec<&Value>)
Set the Field
’s values.