Skip to main content

FormField

Struct FormField 

Source
pub struct FormField {
    pub label: String,
    pub input: TextInputState,
    pub error: Option<String>,
    pub trigger: ValidateTrigger,
    /* private fields */
}
Expand description

A single form field with a label, an input, and its own validators.

Attach validators with the chainable validate builder (multiple allowed); choose when they run with on_change / on_blur. Context::form_field runs them automatically per trigger.

§Example

let field = FormField::new("Email")
    .placeholder("you@example.com")
    .validate(validators::required("required"))
    .validate(validators::email());

Fields§

§label: String

Field label shown above the input.

§input: TextInputState

Text input state for this field.

§error: Option<String>

Validation error shown below the input when present.

§trigger: ValidateTrigger

When the field’s validators run. Defaults to ValidateTrigger::OnBlur.

Implementations§

Source§

impl FormField

Source

pub fn new(label: impl Into<String>) -> Self

Create a new form field with the given label.

Source

pub fn placeholder(self, p: impl Into<String>) -> Self

Set placeholder text for this field’s input.

Source

pub fn validate(self, f: impl Fn(&str) -> Result<(), String> + 'static) -> Self

Attach a validator closure (chainable; call multiple times to stack validators — the first failure becomes the field error).

The closure may capture state, unlike the deprecated positional FormValidator. Built-ins live in validators.

§Example
let field = FormField::new("Name")
    .validate(validators::required("required"))
    .validate(validators::max_len(50, "too long"));
Source

pub fn on_change(self) -> Self

Run this field’s validators on every change (each keystroke).

Source

pub fn on_blur(self) -> Self

Run this field’s validators when it loses focus (the default).

Source

pub fn manual(self) -> Self

Disable automatic validation; the app must call run_validators or FormState::validate_all explicitly.

Source

pub fn validator_count(&self) -> usize

Number of validators attached to this field.

Source

pub fn run_validators(&mut self) -> bool

Run this field’s validators now, setting error to the first failure (or clearing it on success).

Returns true when the field is valid.

§Example
let mut field = FormField::new("Name").validate(validators::required("required"));
assert!(!field.run_validators()); // empty -> error
field.input.value = "Jane".into();
assert!(field.run_validators()); // non-empty -> ok
Source

pub fn validate_async<F>(&mut self, future: F)
where F: Future<Output = Result<(), String>> + Send + 'static,

Available on crate feature async only.

Spawn an asynchronous validation of the current value, replacing any previously pending check.

The future runs on the ambient tokio runtime; its Result is surfaced as error once poll_async (called each frame by Context::form_field) sees it complete.

Requires the async feature.

§Example
let value = field.input.value.clone();
field.validate_async(async move {
    // e.g. hit a "username taken?" endpoint
    if value == "taken" { Err("already taken".into()) } else { Ok(()) }
});
Source

pub fn is_validating(&self) -> bool

Available on crate feature async only.

Whether an async validation is currently in flight.

Requires the async feature.

Source

pub fn poll_async(&mut self) -> bool

Available on crate feature async only.

Poll the in-flight async validation (if any) without blocking.

When the future has resolved, its result is written to error and the pending slot is cleared. Returns true when a result was just applied this call.

Requires the async feature.

Trait Implementations§

Source§

impl Debug for FormField

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FormField

Source§

fn default() -> FormField

Returns the “default value” for a type. 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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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.