let mut fi = zakform::fields::FieldRadio::new("radio");
fi.add_option(zakform::fields::FOption{ value: String::from("F"), label: String::from("Female") });
fi.add_option(zakform::fields::FOption{ value: String::from("M"), label: String::from("Male") });
+ f.add_field(Box::new(fi));
+ let mut fi = zakform::fields::FieldTextArea::new("textarea");
f.add_field(Box::new(fi));
let fs = f.fields();
}
}
+#[derive(Default)]
+pub struct FieldTextArea {
+ field: Field,
+ maxlen: i32,
+ rows: i32,
+}
+
+impl TField for FieldTextArea {
+ fn get_name(&self) -> String {
+ String::from(&self.field.name)
+ }
+
+ fn render(&self) -> String {
+ let mut s = String::new();
+
+ let mut context = tera::Context::new();
+ context.insert("name", &self.field.name);
+ context.insert("label", &self.field.label);
+ context.insert("maxlen", &self.maxlen);
+ context.insert("rows", &self.rows);
+ context.insert("disabled", &self.field.disabled);
+ context.insert("invisible", &self.field.invisible);
+ context.insert("help", &self.field.help);
+ context.insert("value", &self.field.value);
+ s.push_str(self.field.tmpl.render("field", &context).unwrap().as_str());
+
+ s
+ }
+
+ fn any(&mut self) -> &mut dyn Any {
+ self
+ }
+}
+
+impl FieldTextArea {
+ pub fn new(name: &str) -> Self {
+ let mut f: FieldTextArea = Default::default();
+
+ f.field.name = String::from(name);
+ f.field.label = String::from(name);
+
+ f.rows = 5;
+
+ match f.field.tmpl.add_raw_template("field", r#"{% if label %}<div class="mb-3">
+ <label for="{{ name }}" class="form-label">{{ label }}</label>{% endif %}
+ <textarea class="form-control{%if help %} is-invalid{% endif %}" {% if maxlen > 0 %}maxlength="{{ maxlen }}"{% endif %}
+ name="{{ name }}" id="{{ name }}" rows="{{ rows }}">{{ value }}</textarea>
+ {% if help %}<div id="helpBox_{{ name }}_" class="invalid-feedback">{{ help }}</div>{% endif %}
+ {% if label %}</div>{% endif %}"#) {
+Err(e) => { println!("{:?}", e) },
+_ => {},
+};
+
+ f
+ }
+}
+
#[derive(Serialize)]
pub struct FOption {
pub value: String,