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
Inherent implementation of FormState.
impl FormState
Inherent implementation of FormState.
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.get_values().get().get(name).cloned().unwrap_or_default()
instead, so the closure actually subscribes.
§Arguments
&'static str- Shared reference to a'static str.
§Returns
String- AStringvalue.
Sourcepub fn is_touched(&self, name: &'static str) -> bool
pub fn is_touched(&self, name: &'static str) -> bool
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.
§Arguments
&'static str- Shared reference to a'static str.&str- Shared reference to astr.
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”.
§Arguments
&'static str- Shared reference to a'static str.
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.
§Returns
bool- A boolean.
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.get_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.
§Returns
bool- A boolean.
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.
§Returns
usize- Count of currently-registered errors.