Skip to main content

FormState

Struct FormState 

Source
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.

Source

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 - A String value.
Source

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.

§Arguments
  • &'static str - Shared reference to a 'static str.
§Returns
  • String - A String value.
Source

pub fn is_touched(&self, name: &'static str) -> bool

Returns true if the user has interacted with the named field.

§Arguments
  • &'static str - Field name.
§Returns
  • bool - true when the field has been touched.
Source

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 a str.
Source

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.
Source

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 returns Some(error_message) or None.
§Returns
  • bool - A boolean.
Source

pub fn submit<F>( &self, validators: &HashMap<&'static str, Validator>, on_submit: F, ) -> bool
where F: FnOnce(&HashMap<&'static str, String>),

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.
Source

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.

Source

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.
Source§

impl FormState

Source

pub fn get_values(&self) -> &Signal<HashMap<&'static str, String>>

Source

pub fn get_mut_values(&mut self) -> &mut Signal<HashMap<&'static str, String>>

Source

pub fn set_values( &mut self, val: Signal<HashMap<&'static str, String>>, ) -> &mut Self

Source

pub fn get_errors(&self) -> &Signal<HashMap<&'static str, String>>

Source

pub fn get_mut_errors(&mut self) -> &mut Signal<HashMap<&'static str, String>>

Source

pub fn set_errors( &mut self, val: Signal<HashMap<&'static str, String>>, ) -> &mut Self

Source

pub fn get_touched(&self) -> &Signal<HashSet<&'static str>>

Source

pub fn get_mut_touched(&mut self) -> &mut Signal<HashSet<&'static str>>

Source

pub fn set_touched(&mut self, val: Signal<HashSet<&'static str>>) -> &mut Self

Source

pub fn get_submitting(&self) -> &Signal<bool>

Source

pub fn get_mut_submitting(&mut self) -> &mut Signal<bool>

Source

pub fn set_submitting(&mut self, val: Signal<bool>) -> &mut Self

Source§

impl FormState

Source

pub fn new( values: Signal<HashMap<&'static str, String>>, errors: Signal<HashMap<&'static str, String>>, touched: Signal<HashSet<&'static str>>, submitting: Signal<bool>, ) -> Self

Trait Implementations§

Source§

impl Clone for FormState

Source§

fn clone(&self) -> FormState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more