use zakform::filters;
+use std::collections::HashMap;
+
use config::{Config, ConfigError};
-use serde_derive::Deserialize;
+use serde_derive::{Serialize,Deserialize};
use actix_web::{
body::BoxBody,
settings: Settings,
}
-#[derive(Deserialize)]
+#[derive(Serialize,Deserialize)]
struct FormData {
name: String,
notes: String,
let mut f = zakform::Form::new();
+ let strform = serde_json::to_string(&form).unwrap();
+ println!("{:#?}",strform);
+ let map: HashMap<String, String> = serde_json::from_str(&strform).unwrap();
+
let mut fi = zakform::Field::new(zakform::FType::Text, "name");
fi.set_label("Name");
- fi.set_value(form.name.as_str());
+ fi.set_value(map.get(fi.name().as_str()).unwrap().as_str());
fi.add_filter(filters::filter_uppercase);
fi.filter();
f.add_field(fi);
let mut fi = zakform::Field::new(zakform::FType::TextArea, "notes");
fi.set_label("Notes");
fi.set_rows(15);
- fi.set_value(form.notes.as_str());
+ fi.set_value(map.get(fi.name().as_str()).unwrap().as_str());
fi.add_filter(filters::filter_trim);
fi.filter();
f.add_field(fi);
s.push_str(fi.render().as_str());
}
+ let js: HashMap<String, String> = serde_json::from_str(f.as_json_string().as_str()).unwrap();
+ println!("{:#?}", js);
+
s.push_str(r#"<button type="submit">Send</button>"#);
s.push_str("</form>\n");
Err(())
}
+
+ pub fn as_json_string(&mut self) -> String {
+ let mut s = String::from("{");
+
+ for f in &mut self.fields {
+ s.push_str(format!(r#""{}": "{}","#, f.name, f.value).as_str());
+ }
+
+ s.truncate(s.len() - 1);
+ s.push_str("}");
+
+ s
+ }
}