pub struct FormState { /* private fields */ }Expand description
A controlled-form helper. Create one per form (typically stored in a
component’s local state / captured by its closures) and bind fields to
Input/Textarea/Select/Radio/Checkbox nodes via
FormState::field/FormState::checkbox for the current value and
FormState::set/FormState::set_checkbox (typically from
.on_input/.on_toggle) to write new values back.
Implementations§
Source§impl FormState
impl FormState
pub fn new() -> Self
Sourcepub fn field(&self, name: &str, default: impl Into<String>) -> Signal<String>
pub fn field(&self, name: &str, default: impl Into<String>) -> Signal<String>
The value signal for string field name, creating it with default
on first access. Read this to bind a node’s current value.
Sourcepub fn validate(&self, name: &str, f: impl Fn(&str) -> Option<String> + 'static)
pub fn validate(&self, name: &str, f: impl Fn(&str) -> Option<String> + 'static)
Registers a validator for name, run against every new value passed
to FormState::set. f returns Some(message) to reject the value
(recorded, but the value is still stored — callers can still see what
the user typed) or None when it’s valid.
Sourcepub fn set(&self, name: &str, value: impl Into<String>)
pub fn set(&self, name: &str, value: impl Into<String>)
Sets name’s value (creating the field with an empty default if this
is the first write) and re-runs its validator.
Sourcepub fn error(&self, name: &str) -> Option<String>
pub fn error(&self, name: &str) -> Option<String>
The current validation error for string field name, if any.
Sourcepub fn checkbox(&self, name: &str, default: bool) -> Signal<bool>
pub fn checkbox(&self, name: &str, default: bool) -> Signal<bool>
The boolean field backing a Checkbox, creating it with default on
first access.
Sourcepub fn set_checkbox(&self, name: &str, value: bool)
pub fn set_checkbox(&self, name: &str, value: bool)
Sets boolean field name (creating it if this is the first write).