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: StringThe field name used as the form data key.
required: boolWhether at least one selection is required.
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.
queryset: Vec<T>The list of model instances to choose from.
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};
// Define a simple Tag model
#[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(())
}
}
// Create a queryset with sample tags
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));
// Test with multiple selections
let result = field.clean(Some(&json!(["1", "2"])));
assert!(result.is_ok());Trait Implementations§
Source§impl<T: FormModel> FormField for ModelMultipleChoiceField<T>
impl<T: FormModel> FormField for ModelMultipleChoiceField<T>
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<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