Skip to main content

ModelMultipleChoiceField

Struct ModelMultipleChoiceField 

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

Source

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

pub fn required(self, required: bool) -> Self

Source

pub fn help_text(self, text: impl Into<String>) -> Self

Source

pub fn initial(self, value: Value) -> Self

Source

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>

Source§

fn name(&self) -> &str

Source§

fn label(&self) -> Option<&str>

Source§

fn widget(&self) -> &Widget

Source§

fn required(&self) -> bool

Source§

fn initial(&self) -> Option<&Value>

Source§

fn help_text(&self) -> Option<&str>

Source§

fn clean(&self, value: Option<&Value>) -> FieldResult<Value>

Source§

fn has_changed(&self, initial: Option<&Value>, data: Option<&Value>) -> bool

Check if the field value has changed from its initial value
Source§

fn error_messages(&self) -> HashMap<ErrorType, String>

Get custom error messages for this field

Auto Trait Implementations§

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, 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.