pub struct ModelChoiceField<T: FormModel> {
pub name: String,
pub required: bool,
pub error_messages: HashMap<String, String>,
pub widget: Widget,
pub help_text: String,
pub initial: Option<Value>,
pub queryset: Vec<T>,
pub empty_label: Option<String>,
/* private fields */
}Expand description
A field for selecting a single model instance from a queryset
This field displays model instances as choices in a select widget.
Fields§
§name: String§required: bool§error_messages: HashMap<String, String>§widget: Widget§help_text: String§initial: Option<Value>§queryset: Vec<T>§empty_label: Option<String>Implementations§
Source§impl<T: FormModel> ModelChoiceField<T>
impl<T: FormModel> ModelChoiceField<T>
Sourcepub fn new(name: impl Into<String>, queryset: Vec<T>) -> Self
pub fn new(name: impl Into<String>, queryset: Vec<T>) -> Self
Create a new ModelChoiceField
§Examples
use reinhardt_forms::fields::ModelChoiceField;
use reinhardt_forms::FormField;
use reinhardt_forms::FormModel;
use serde_json::{json, Value};
#[derive(Clone)]
struct Category {
id: i32,
name: String,
}
impl FormModel for Category {
fn field_names() -> Vec<String> {
vec!["id".to_string(), "name".to_string()]
}
fn get_field(&self, name: &str) -> Option<Value> {
match name {
"id" => Some(json!(self.id)),
"name" => Some(json!(self.name)),
_ => None,
}
}
fn set_field(&mut self, _name: &str, _value: Value) -> Result<(), String> {
Ok(())
}
fn save(&mut self) -> Result<(), String> {
Ok(())
}
}
let categories = vec![
Category { id: 1, name: "Technology".to_string() },
Category { id: 2, name: "Science".to_string() },
];
let field = ModelChoiceField::new("category", categories);
assert_eq!(field.name(), "category");
assert!(FormField::required(&field));pub fn required(self, required: bool) -> Self
pub fn help_text(self, text: impl Into<String>) -> Self
pub fn initial(self, value: Value) -> Self
pub fn empty_label(self, label: Option<String>) -> Self
pub fn error_message( self, error_type: impl Into<String>, message: impl Into<String>, ) -> Self
Trait Implementations§
Source§impl<T: FormModel> FormField for ModelChoiceField<T>
impl<T: FormModel> FormField for ModelChoiceField<T>
fn name(&self) -> &str
fn label(&self) -> Option<&str>
fn widget(&self) -> &Widget
fn required(&self) -> bool
fn initial(&self) -> Option<&Value>
fn help_text(&self) -> Option<&str>
fn clean(&self, value: Option<&Value>) -> FieldResult<Value>
Auto Trait Implementations§
impl<T> Freeze for ModelChoiceField<T>
impl<T> RefUnwindSafe for ModelChoiceField<T>where
T: RefUnwindSafe,
impl<T> Send for ModelChoiceField<T>
impl<T> Sync for ModelChoiceField<T>
impl<T> Unpin for ModelChoiceField<T>where
T: Unpin,
impl<T> UnsafeUnpin for ModelChoiceField<T>
impl<T> UnwindSafe for ModelChoiceField<T>where
T: UnwindSafe,
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