pub struct ModelMultipleChoiceField<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>,
/* private fields */
}Expand description
A field for selecting multiple model instances from a queryset
This field displays model instances as choices in a multiple select widget.
Fields§
§name: String§required: bool§error_messages: HashMap<String, String>§widget: Widget§help_text: String§initial: Option<Value>§queryset: Vec<T>Implementations§
Source§impl<T: FormModel> ModelMultipleChoiceField<T>
impl<T: FormModel> ModelMultipleChoiceField<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 ModelMultipleChoiceField
§Examples
use reinhardt_forms::fields::ModelMultipleChoiceField;
use reinhardt_forms::FormField;
use reinhardt_forms::FormModel;
use serde_json::{json, Value};
#[derive(Clone)]
struct Tag {
id: i32,
name: String,
}
impl FormModel for Tag {
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 tags = vec![
Tag { id: 1, name: "rust".to_string() },
Tag { id: 2, name: "programming".to_string() },
Tag { id: 3, name: "web".to_string() },
];
let field = ModelMultipleChoiceField::new("tags", tags);
assert_eq!(field.name(), "tags");
assert!(FormField::required(&field));
let result = field.clean(Some(&json!(["1", "2"])));
assert!(result.is_ok());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 error_message( self, error_type: impl Into<String>, message: impl Into<String>, ) -> Self
Trait Implementations§
Source§impl<T: FormModel> FormField for ModelMultipleChoiceField<T>
impl<T: FormModel> FormField for ModelMultipleChoiceField<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 ModelMultipleChoiceField<T>
impl<T> RefUnwindSafe for ModelMultipleChoiceField<T>where
T: RefUnwindSafe,
impl<T> Send for ModelMultipleChoiceField<T>
impl<T> Sync for ModelMultipleChoiceField<T>
impl<T> Unpin for ModelMultipleChoiceField<T>where
T: Unpin,
impl<T> UnsafeUnpin for ModelMultipleChoiceField<T>
impl<T> UnwindSafe for ModelMultipleChoiceField<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