Skip to main content

BoundField

Struct BoundField 

Source
pub struct BoundField<'a> { /* private fields */ }
Expand description

BoundField represents a field bound to form data

Implementations§

Source§

impl<'a> BoundField<'a>

Source

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));
Source

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");
Source

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");
Source

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");
Source

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"));
Source

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));
Source

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");
Source

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());
Source

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));
Source

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"));
Source

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> 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> Same for T

Source§

type Output = T

Should always be Self
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.