green_barrel/fields/
text.rs

1//! A field for entering a **text** string.
2//! For Html <input type="**text**|**radio**" and **textarea**(multiline=true)
3
4use core::fmt::Debug;
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize, Clone, Debug)]
8pub struct TextField {
9    /// The value is determined automatically.
10    /// Format: "model-name--field-name".
11    pub id: String,
12    /// Web form field name.
13    pub label: String,
14    /// Field type.
15    pub field_type: String,
16    /// `<input type="text|radio">`.
17    pub input_type: String,
18    /// true - for textarea.
19    pub multiline: bool,
20    /// The value is determined automatically.
21    pub name: String,
22    /// Sets the value of an element.
23    pub value: Option<String>,
24    /// Value by default.
25    pub default: Option<String>,
26    /// Displays prompt text.
27    pub placeholder: String,
28    /// A regular expression to validate the value.
29    pub regex: String,
30    /// To customize error message.
31    pub regex_err_msg: String,
32    /// The minimum number of characters allowed in the text.
33    pub minlength: usize,
34    /// The maximum number of characters allowed in the text.
35    pub maxlength: usize,
36    /// Mandatory field.
37    pub required: bool,
38    /// The unique value of a field in a collection.
39    pub unique: bool,
40    /// Blocks access and modification of the element.
41    pub disabled: bool,
42    /// Specifies that the field cannot be modified by the user.
43    pub readonly: bool,
44    /// For Html `<input type="radio" />`.
45    /// Format: [(Value, Title), ...]
46    pub choices: Vec<(String, String)>,
47    /// Hide field from user.
48    pub is_hide: bool,
49    /// Example: `r# "autofocus tabindex="some number" size="some number"#`.
50    pub other_attrs: String,
51    /// Example: "class-name-1 class-name-2".
52    pub css_classes: String,
53    /// Additional explanation for the user.
54    pub hint: String,
55    /// Warning information.
56    pub warning: String,
57    /// The value is determined automatically.
58    pub errors: Vec<String>,
59    /// To optimize field traversal in the `paladins/check()` method.
60    /// Hint: It is recommended not to change.
61    pub group: u32,
62}
63
64impl Default for TextField {
65    fn default() -> Self {
66        Self {
67            id: String::new(),
68            label: String::new(),
69            field_type: String::from("TextField"),
70            input_type: String::from("text"), // text|radio
71            multiline: false,                 // true - for textarea.
72            name: String::new(),
73            value: None,
74            default: None,
75            placeholder: String::new(),
76            regex: String::new(),
77            regex_err_msg: String::new(),
78            minlength: 0,
79            maxlength: 256,
80            required: false,
81            unique: false,
82            disabled: false,
83            readonly: false,
84            // For Html <input type="radio" />.
85            // Format: [(value, Title), ...]
86            choices: Vec::new(),
87            is_hide: false,
88            other_attrs: String::new(),
89            css_classes: String::new(),
90            hint: String::new(),
91            warning: String::new(),
92            errors: Vec::new(),
93            group: 1,
94        }
95    }
96}
97
98impl TextField {
99    /// Getter
100    pub fn get(&self) -> Option<String> {
101        self.value.clone()
102    }
103    /// Setter
104    pub fn set(&mut self, value: &str) {
105        self.value = Some(String::from(value));
106    }
107}