pub struct BoundField<'a> { /* private fields */ }Expand description
BoundField represents a field bound to form data
Implementations§
Source§impl<'a> BoundField<'a>
impl<'a> BoundField<'a>
Sourcepub fn new(
form_name: String,
field: &'a dyn FormField,
data: Option<&'a Value>,
errors: &'a [String],
prefix: &'a str,
) -> Self
pub fn new( form_name: String, field: &'a dyn FormField, data: Option<&'a Value>, errors: &'a [String], prefix: &'a str, ) -> Self
Documentation for new
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let field: Box<dyn FormField> = Box::new(CharField::new("name".to_string()));
let data = serde_json::json!("John");
let errors = vec![];
let bound = BoundField::new("my_form".to_string(), field.as_ref(), Some(&data), &errors, "");
assert_eq!(bound.name(), "name");
assert_eq!(bound.value(), Some(&data));Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Get the field name
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let field: Box<dyn FormField> = Box::new(CharField::new("email".to_string()));
let bound = BoundField::new("form".to_string(), field.as_ref(), None, &[], "");
assert_eq!(bound.name(), "email");Sourcepub fn html_name(&self) -> String
pub fn html_name(&self) -> String
Get the HTML name attribute (with prefix)
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let field: Box<dyn FormField> = Box::new(CharField::new("email".to_string()));
// Without prefix
let bound = BoundField::new("form".to_string(), field.as_ref(), None, &[], "");
assert_eq!(bound.html_name(), "email");
// With prefix
let bound_prefixed = BoundField::new("form".to_string(), field.as_ref(), None, &[], "user");
assert_eq!(bound_prefixed.html_name(), "user-email");Sourcepub fn id_for_label(&self) -> String
pub fn id_for_label(&self) -> String
Get the HTML id attribute
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let field: Box<dyn FormField> = Box::new(CharField::new("username".to_string()));
let bound = BoundField::new("form".to_string(), field.as_ref(), None, &[], "profile");
assert_eq!(bound.id_for_label(), "id_profile-username");Sourcepub fn label(&self) -> Option<&str>
pub fn label(&self) -> Option<&str>
Get the field label
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let mut field = CharField::new("name".to_string());
field.label = Some("Full Name".to_string());
let field_box: Box<dyn FormField> = Box::new(field);
let bound = BoundField::new("form".to_string(), field_box.as_ref(), None, &[], "");
assert_eq!(bound.label(), Some("Full Name"));Sourcepub fn value(&self) -> Option<&Value>
pub fn value(&self) -> Option<&Value>
Get the field value
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let field: Box<dyn FormField> = Box::new(CharField::new("name".to_string()));
let data = serde_json::json!("Alice");
let bound = BoundField::new("form".to_string(), field.as_ref(), Some(&data), &[], "");
assert_eq!(bound.value(), Some(&data));Sourcepub fn errors(&self) -> &[String]
pub fn errors(&self) -> &[String]
Get field errors
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let field: Box<dyn FormField> = Box::new(CharField::new("email".to_string()));
let errors = vec!["Invalid email format".to_string(), "Email is required".to_string()];
let bound = BoundField::new("form".to_string(), field.as_ref(), None, &errors, "");
assert_eq!(bound.errors().len(), 2);
assert_eq!(bound.errors()[0], "Invalid email format");Sourcepub fn has_errors(&self) -> bool
pub fn has_errors(&self) -> bool
Check if field has errors
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let field: Box<dyn FormField> = Box::new(CharField::new("username".to_string()));
// Without errors
let bound_ok = BoundField::new("form".to_string(), field.as_ref(), None, &[], "");
assert!(!bound_ok.has_errors());
// With errors
let errors = vec!["Username is required".to_string()];
let bound_err = BoundField::new("form".to_string(), field.as_ref(), None, &errors, "");
assert!(bound_err.has_errors());Sourcepub fn widget(&self) -> &Widget
pub fn widget(&self) -> &Widget
Get the widget
§Examples
use reinhardt_forms::{BoundField, CharField, EmailField, FormField, Widget};
let field: Box<dyn FormField> = Box::new(CharField::new("name".to_string()));
let bound = BoundField::new("form".to_string(), field.as_ref(), None, &[], "");
assert!(matches!(bound.widget(), Widget::TextInput));
let email_field: Box<dyn FormField> = Box::new(EmailField::new("email".to_string()));
let email_bound = BoundField::new("form".to_string(), email_field.as_ref(), None, &[], "");
assert!(matches!(email_bound.widget(), Widget::EmailInput));Sourcepub fn help_text(&self) -> Option<&str>
pub fn help_text(&self) -> Option<&str>
Get help text
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let mut field = CharField::new("password".to_string());
field.help_text = Some("Must be at least 8 characters".to_string());
let field_box: Box<dyn FormField> = Box::new(field);
let bound = BoundField::new("form".to_string(), field_box.as_ref(), None, &[], "");
assert_eq!(bound.help_text(), Some("Must be at least 8 characters"));Sourcepub fn is_required(&self) -> bool
pub fn is_required(&self) -> bool
Check if field is required
§Examples
use reinhardt_forms::{BoundField, CharField, FormField};
let mut field = CharField::new("name".to_string());
field.required = true;
let field_box: Box<dyn FormField> = Box::new(field);
let bound = BoundField::new("form".to_string(), field_box.as_ref(), None, &[], "");
assert!(bound.is_required());
let mut optional_field = CharField::new("nickname".to_string());
optional_field.required = false;
let optional_box: Box<dyn FormField> = Box::new(optional_field);
let optional_bound = BoundField::new("form".to_string(), optional_box.as_ref(), None, &[], "");
assert!(!optional_bound.is_required());Auto Trait Implementations§
impl<'a> !RefUnwindSafe for BoundField<'a>
impl<'a> !UnwindSafe for BoundField<'a>
impl<'a> Freeze for BoundField<'a>
impl<'a> Send for BoundField<'a>
impl<'a> Sync for BoundField<'a>
impl<'a> Unpin for BoundField<'a>
impl<'a> UnsafeUnpin for BoundField<'a>
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