pub struct FormState { /* private fields */ }Expand description
The aggregate form state.
Constructed once per <form> via App::use_form(),
threaded through the form’s inputs and the submit
handler. Cheap to Clone (the four internal signals are
each Copy-by-pointer).
§Field reactivity
values, errors, touched, and submitting are all
Signals, so any html! body that calls
state.values().get() (or any of the other three) inside
its render closure re-renders when the corresponding
signal changes. There is no manual subscribe API; the
reactive read IS the subscription.
Implementations§
Source§impl FormState
impl FormState
Sourcepub fn values(&self) -> Signal<HashMap<&'static str, String>>
pub fn values(&self) -> Signal<HashMap<&'static str, String>>
Returns a Signal clone of the values map.
Read with .get() inside a render closure to
subscribe to value changes. Callers needing a
single field’s value should use
FormState::field(name) instead, which is a
convenience that avoids re-reading the whole map.
Sourcepub fn errors(&self) -> Signal<HashMap<&'static str, String>>
pub fn errors(&self) -> Signal<HashMap<&'static str, String>>
Returns a Signal clone of the errors map.
Sourcepub fn touched(&self) -> Signal<HashSet<&'static str>>
pub fn touched(&self) -> Signal<HashSet<&'static str>>
Returns a Signal clone of the touched set.
Sourcepub fn submitting(&self) -> Signal<bool>
pub fn submitting(&self) -> Signal<bool>
Returns a Signal<bool> clone of the submitting
flag.
Sourcepub fn field(&self, name: &'static str) -> String
pub fn field(&self, name: &'static str) -> String
Returns the current value of the named field, or
"" if the field has never been set.
This is a snapshot read, not a subscription —
callers inside a render closure that want to
re-render on value changes should use
state.values().get().get(name).cloned().unwrap_or_default()
instead, so the closure actually subscribes.
Sourcepub fn error(&self, name: &'static str) -> String
pub fn error(&self, name: &'static str) -> String
Returns the current error for the named field, or
"" if the field has no error.
Snapshot read — see field for the subscription
caveat.
Sourcepub fn is_touched(&self, name: &'static str) -> bool
pub fn is_touched(&self, name: &'static str) -> bool
Returns true if the user has interacted with the
named field.
Sourcepub fn set_field(&self, name: &'static str, value: &str)
pub fn set_field(&self, name: &'static str, value: &str)
Sets the value of the named field.
Marks the field as touched (mirroring the
oninput event that triggered the call) and
clears any prior error for the field. The error
clear is a UX choice — the next validate call
will repopulate it if the new value is still
invalid.
Sourcepub fn touch(&self, name: &'static str)
pub fn touch(&self, name: &'static str)
Marks the named field as touched without changing
its value. Used by onblur handlers — “the user
left this field, so it counts as interacted”.
Sourcepub fn validate(&self, validators: &HashMap<&'static str, Validator>) -> bool
pub fn validate(&self, validators: &HashMap<&'static str, Validator>) -> bool
Runs every validator in validators and updates the
errors signal.
Returns true if every field validated
successfully (i.e. every validator returned
None), false otherwise. The errors signal is
always updated, regardless of return value —
callers should call validate and then branch on
the boolean.
Fields with no validator are silently skipped — they cannot produce an error.
§Arguments
&HashMap<&'static str, Validator>- Per-field validator map. Each validator is a closure that takes the current value and returnsSome(error_message)orNone.
Sourcepub fn submit<F>(
&self,
validators: &HashMap<&'static str, Validator>,
on_submit: F,
) -> bool
pub fn submit<F>( &self, validators: &HashMap<&'static str, Validator>, on_submit: F, ) -> bool
Runs the user-supplied submit handler if all validators pass.
Sets submitting to true for the duration of the
call (so a disabled={state.submitting().get()}
button stays disabled until the handler returns),
then resets it to false. If validators were
supplied AND at least one field failed validation,
the submit handler is NOT invoked and submitting
is left false.
Returns true if the handler was invoked,
false if validation failed and the handler was
skipped.
§Arguments
&HashMap<&'static str, Validator>- Validators to run before invoking the handler. Pass an empty map to skip validation entirely (the handler always runs).impl FnOnce(&HashMap<&'static str, String>)- The submit handler. Receives the current values map by reference — clone what you need to keep past the call.
Sourcepub fn reset(&self)
pub fn reset(&self)
Clears values, errors, and touched state. Leaves
submitting untouched (it should already be
false).
Useful for “form submitted successfully, reset for the next entry” UX flows.
Sourcepub fn error_count(&self) -> usize
pub fn error_count(&self) -> usize
Returns the number of fields that currently have a non-empty error. Useful for “submit button stays disabled until form is valid” without re-running validation.