Crates.io | rooting_forms_proc_macros |
lib.rs | rooting_forms_proc_macros |
version | 0.0.4 |
source | src |
created_at | 2023-10-24 02:56:56.134822 |
updated_at | 2024-02-22 08:32:57.349921 |
description | Generates HTML (rooting) forms from structures (helper crate) |
homepage | https://github.com/andrewbaxter/rooting-forms |
repository | https://github.com/andrewbaxter/rooting-forms |
max_upload_size | |
id | 1012039 |
size | 21,767 |
This provides traits and a derive macro for generating rooting HTML forms from structs and enums.
Example
#[derive(rooting_forms::Form)]
struct Creds {
#[title("Your username")]
username: String,
#[title("Your password")]
password: rooting_forms::Password,
}
...
let ok_button = el("button").text("Ok");
let (creds_form, creds_form_state) = Creds::new_form("", None);
ok_button.ref_on("click", move |_| {
let Ok(creds) = creds_form_state.parse() else {
return;
};
do_login(creds);
});
let modal =
el(
"div",
).extend(vec![el("div").classes(&["form_grid"]).extend(creds_form.elements), ok_button]);
...
Call ::new_form
on your form type. The first argument can be empty (it's used for aria hints for nested fields but not for top level structs). The second argument is Some
if you want to pre-populate the form from existing data, for instance if you're editing something.
new_form
returns a the form rooting::El
elements and a state object.
Call parse()
on the state object any number of times. It will either return the parsed object or an empty error. Any error messages will be automatically put in error elements near the relevant form fields. The error messages are cleared at the next parse()
.