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: StringField label shown above the input.
input: TextInputStateText input state for this field.
error: Option<String>Validation error shown below the input when present.
trigger: ValidateTriggerWhen the field’s validators run. Defaults to
ValidateTrigger::OnBlur.
Implementations§
Source§impl FormField
impl FormField
Sourcepub fn placeholder(self, p: impl Into<String>) -> Self
pub fn placeholder(self, p: impl Into<String>) -> Self
Set placeholder text for this field’s input.
Sourcepub fn validate(self, f: impl Fn(&str) -> Result<(), String> + 'static) -> Self
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"));Sourcepub fn manual(self) -> Self
pub fn manual(self) -> Self
Disable automatic validation; the app must call
run_validators or
FormState::validate_all explicitly.
Sourcepub fn validator_count(&self) -> usize
pub fn validator_count(&self) -> usize
Number of validators attached to this field.
Sourcepub fn run_validators(&mut self) -> bool
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 -> okSourcepub fn validate_async<F>(&mut self, future: F)
Available on crate feature async only.
pub fn validate_async<F>(&mut self, future: F)
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(()) }
});Sourcepub fn is_validating(&self) -> bool
Available on crate feature async only.
pub fn is_validating(&self) -> bool
async only.Whether an async validation is currently in flight.
Requires the async feature.
Sourcepub fn poll_async(&mut self) -> bool
Available on crate feature async only.
pub fn poll_async(&mut self) -> bool
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.