pub struct ComboField {
pub name: String,
pub required: bool,
pub error_messages: HashMap<String, String>,
pub widget: Widget,
pub help_text: String,
pub initial: Option<Value>,
pub validators: Vec<Box<dyn FormField>>,
}Expand description
A field that combines multiple field validators
ComboField runs all provided validators in sequence and requires all to pass.
§Examples
use reinhardt_forms::fields::ComboField;
use reinhardt_forms::{Field, CharField, EmailField};
use serde_json::json;
// Create validators with constraints
let mut email_field = EmailField::new("email".to_string());
let mut char_field = CharField::new("email".to_string());
char_field.min_length = Some(5);
char_field.max_length = Some(100);
// Combine email validation with length validation
let field = ComboField::new("email")
.add_validator(Box::new(email_field))
.add_validator(Box::new(char_field));
// Valid: passes both email and length checks
let result = field.clean(Some(&json!("user@example.com")));
assert!(result.is_ok());
// Invalid: fails email validation
let result = field.clean(Some(&json!("not-an-email")));
assert!(result.is_err());
// Invalid: too short (less than 5 characters)
let result = field.clean(Some(&json!("a@b")));
assert!(result.is_err());Fields§
§name: StringThe field name used as the form data key.
required: boolWhether this field must be filled in.
error_messages: HashMap<String, String>Custom error messages keyed by error type.
widget: WidgetThe widget type used for rendering this field.
help_text: StringHelp text displayed alongside the field.
initial: Option<Value>Optional initial (default) value for the field.
validators: Vec<Box<dyn FormField>>The list of validator fields that all must pass.
Implementations§
Source§impl ComboField
impl ComboField
Sourcepub fn add_validator(self, validator: Box<dyn FormField>) -> Self
pub fn add_validator(self, validator: Box<dyn FormField>) -> Self
Add a validator field
Trait Implementations§
Source§impl FormField for ComboField
impl FormField for ComboField
Source§fn initial(&self) -> Option<&Value>
fn initial(&self) -> Option<&Value>
Returns the initial (default) value for this field, if any.
Source§fn clean(&self, value: Option<&Value>) -> FieldResult<Value>
fn clean(&self, value: Option<&Value>) -> FieldResult<Value>
Validates and cleans the submitted value, returning the cleaned result.
Auto Trait Implementations§
impl Freeze for ComboField
impl !RefUnwindSafe for ComboField
impl Send for ComboField
impl Sync for ComboField
impl Unpin for ComboField
impl UnsafeUnpin for ComboField
impl !UnwindSafe for ComboField
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more