]> saetta.ns0.it Git - rust/zakform/commitdiff
Form get fields and field by name.
authorAndrea Zagli <azagli@libero.it>
Fri, 6 Jan 2023 09:22:49 +0000 (10:22 +0100)
committerAndrea Zagli <azagli@libero.it>
Fri, 6 Jan 2023 09:22:49 +0000 (10:22 +0100)
examples/form.rs
src/lib.rs

index 22d8774e6a6ac4ad27d3f96d556fea08d6525fab..79096097a6b8e089963045347aafaf15fa790b05 100644 (file)
@@ -39,26 +39,85 @@ struct AppState {
        settings: Settings,
 }
 
+#[derive(Deserialize)]
+struct FormData {
+       name: String,
+       notes: String,
+}
+
+async fn index_post(
+    data: web::Data<AppState>,
+    form: web::Form<FormData>,
+) -> Result<impl Responder, Error> {
+       let mut s = String::new();
+
+       s.push_str("<html>\n");
+       s.push_str("<body>\n");
+
+       s.push_str(r#"<form method="post" action="/">"#);
+
+       let mut f = zakform::Form::new();
+
+       let mut fi = zakform::Field::new(zakform::FType::Text, "name");
+       fi.set_label("Name");
+       fi.set_value(form.name.as_str());
+       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());
+       f.add_field(fi);
+
+       let fs = f.fields();
+       for fi in fs {
+               s.push_str(fi.render().as_str());
+       }
+
+       s.push_str(r#"<button type="submit">Send</button>"#);
+
+       s.push_str("</form>\n");
+
+       s.push_str("</body>\n");
+       s.push_str("</html>");
+
+       Ok(HttpResponse::Ok().body(s))
+}
+
 async fn index(
     data: web::Data<AppState>,
 ) -> Result<impl Responder, Error> {
 
        let mut s = String::new();
 
+       s.push_str("<html>\n");
+       s.push_str("<body>\n");
+
+       s.push_str(r#"<form method="post" action="/">"#);
+
        let mut f = zakform::Form::new();
 
        let mut fi = zakform::Field::new(zakform::FType::Text, "name");
        fi.set_label("Name");
-       fi.set_help("Must not be empty.");
-       s.push_str(fi.render().as_str());
        f.add_field(fi);
 
        let mut fi = zakform::Field::new(zakform::FType::TextArea, "notes");
        fi.set_label("Notes");
        fi.set_rows(15);
-       s.push_str(fi.render().as_str());
        f.add_field(fi);
 
+       let fs = f.fields();
+       for fi in fs {
+               s.push_str(fi.render().as_str());
+       }
+
+       s.push_str(r#"<button type="submit">Send</button>"#);
+
+       s.push_str("</form>\n");
+
+       s.push_str("</body>\n");
+       s.push_str("</html>");
+
        Ok(HttpResponse::Ok().body(s))
 }
 
@@ -82,7 +141,9 @@ async fn main() -> std::io::Result<()> {
                                settings: Settings::new().unwrap(),
                        }))
             .wrap(middleware::Logger::default()) // enable logger
-            .service(web::resource("/").route(web::get().to(index)))
+            .service(web::resource("/")
+                                        .route(web::get().to(index))
+                                        .route(web::post().to(index_post)))
             .service(web::scope("").wrap(error_handlers()))
     })
     .bind((appstate.settings.http.bind_ip, appstate.settings.http.bind_port))?
index 3a38bd240638e7451b45c43fb5bb7e567b4b7a05..689f40f9858fe149768de18c9b95f5cf70c228ce 100644 (file)
@@ -143,4 +143,18 @@ impl Form {
        pub fn add_field(&mut self, field: Field) {
                self.fields.push(field);
        }
+
+       pub fn fields(&self) -> &Vec<Field> {
+               &self.fields
+       }
+
+       pub fn get_field(&mut self, field_name: &str) -> Result<&mut Field, ()> {
+               for f in &mut self.fields {
+                       if f.name.as_str() == field_name {
+                               return Ok(f);
+                       }
+               }
+
+               Err(())
+       }
 }