green_barrel/fields/
choice_text_mult_dyn.rs

1//! Type of selective field with dynamic addition of elements.
2//! For simulate relationship Many-to-Many.
3//! Elements are added via the `ModelName::update_dyn_field()` method.
4
5use core::fmt::Debug;
6use serde::{Deserialize, Serialize};
7
8#[derive(Serialize, Deserialize, Clone, Debug)]
9pub struct ChoiceTextMultDynField {
10    pub id: String, // The value is determined automatically. Format: "model-name--field-name".
11    pub label: String, // Web form field name.
12    pub field_type: String, // Field type.
13    pub name: String, // The value is determined automatically.
14    pub value: Option<Vec<String>>, // Sets the value of an element.
15    pub placeholder: String, // Displays prompt text.
16    pub required: bool, // Mandatory field.
17    pub unique: bool, // The unique value of a field in a collection.
18    pub disabled: bool, // Blocks access and modification of the element.
19    pub readonly: bool, // Specifies that the field cannot be modified by the user.
20    pub multiple: String, // Specifies that multiple options can be selected at once. Changing the default value is not recommended.
21    pub choices: Vec<(String, String)>, // Elements are added via the ModelName::update_dyn_field() method.
22    pub is_hide: bool,                  // Hide field from user.
23    /// Example: `r# "autofocus tabindex="some number" size="some number"#`.    
24    pub other_attrs: String,
25    pub css_classes: String, // Example: "class-name-1 class-name-2".
26    pub hint: String,        // Additional explanation for the user.
27    pub warning: String,     // Warning information.
28    pub errors: Vec<String>, // The value is determined automatically.
29    pub group: u32, // To optimize field traversal in the `paladins/check()` method. Hint: It is recommended not to change.
30}
31
32impl Default for ChoiceTextMultDynField {
33    fn default() -> Self {
34        Self {
35            id: String::new(),
36            label: String::new(),
37            field_type: String::from("ChoiceTextMultDynField"),
38            name: String::new(),
39            value: None,
40            placeholder: String::new(),
41            required: false,
42            unique: false,
43            disabled: false,
44            readonly: false,
45            // Changing the default value is not recommended.
46            multiple: String::from("multiple"),
47            choices: Vec::new(),
48            is_hide: false,
49            other_attrs: String::new(),
50            css_classes: String::new(),
51            hint: String::new(),
52            warning: String::new(),
53            errors: Vec::new(),
54            group: 7,
55        }
56    }
57}
58
59impl ChoiceTextMultDynField {
60    pub fn get(&self) -> Option<Vec<String>> {
61        self.value.clone()
62    }
63    pub fn set(&mut self, value: Vec<&str>) {
64        let value = value
65            .iter()
66            .map(|item| item.to_string())
67            .collect::<Vec<String>>();
68        self.value = Some(value);
69    }
70}