Skip to main content

actix_web_admin/types/
field.rs

1/// Type of input field in the admin form.
2#[derive(Debug, Clone, serde::Serialize)]
3pub enum FieldType {
4    Text,
5    TextArea { rows: u8 },
6    Number,
7    Email,
8    Password,
9    Boolean,
10    Date,
11    Select { options: Vec<(String, String)> },
12}
13
14/// Configuration for a field in the admin create/edit form.
15#[derive(Debug, Clone, serde::Serialize)]
16pub struct FormField {
17    pub key: String,
18    pub label: String,
19    pub field_type: FieldType,
20    pub required: bool,
21    pub placeholder: Option<String>,
22    pub help_text: Option<String>,
23}
24
25impl FormField {
26    pub fn text(key: &str, label: &str) -> Self {
27        Self {
28            key: key.to_string(),
29            label: label.to_string(),
30            field_type: FieldType::Text,
31            required: false,
32            placeholder: None,
33            help_text: None,
34        }
35    }
36
37    pub fn number(key: &str, label: &str) -> Self {
38        Self {
39            key: key.to_string(),
40            label: label.to_string(),
41            field_type: FieldType::Number,
42            required: false,
43            placeholder: None,
44            help_text: None,
45        }
46    }
47
48    pub fn email(key: &str, label: &str) -> Self {
49        Self {
50            key: key.to_string(),
51            label: label.to_string(),
52            field_type: FieldType::Email,
53            required: false,
54            placeholder: None,
55            help_text: None,
56        }
57    }
58
59    pub fn password(key: &str, label: &str) -> Self {
60        Self {
61            key: key.to_string(),
62            label: label.to_string(),
63            field_type: FieldType::Password,
64            required: false,
65            placeholder: None,
66            help_text: None,
67        }
68    }
69
70    pub fn textarea(key: &str, label: &str, rows: u8) -> Self {
71        Self {
72            key: key.to_string(),
73            label: label.to_string(),
74            field_type: FieldType::TextArea { rows },
75            required: false,
76            placeholder: None,
77            help_text: None,
78        }
79    }
80
81    pub fn boolean(key: &str, label: &str) -> Self {
82        Self {
83            key: key.to_string(),
84            label: label.to_string(),
85            field_type: FieldType::Boolean,
86            required: false,
87            placeholder: None,
88            help_text: None,
89        }
90    }
91
92    pub fn select(key: &str, label: &str, options: Vec<(&str, &str)>) -> Self {
93        let options = options
94            .into_iter()
95            .map(|(k, v)| (k.to_string(), v.to_string()))
96            .collect();
97        Self {
98            key: key.to_string(),
99            label: label.to_string(),
100            field_type: FieldType::Select { options },
101            required: false,
102            placeholder: None,
103            help_text: None,
104        }
105    }
106
107    pub fn required(mut self) -> Self {
108        self.required = true;
109        self
110    }
111
112    pub fn placeholder(mut self, p: &str) -> Self {
113        self.placeholder = Some(p.to_string());
114        self
115    }
116
117    pub fn help(mut self, h: &str) -> Self {
118        self.help_text = Some(h.to_string());
119        self
120    }
121}