diff --git a/assets/site.css b/assets/site.css index 92e992b..c4adaf7 100644 --- a/assets/site.css +++ b/assets/site.css @@ -123,7 +123,8 @@ section { margin-top: 2em; } -p.form-error { +p.form-error, +div.form-error { background-color: var(--clr-error-surface); border: 2pt solid var(--clr-error-primary-0); color: var(--clr-error-primary-40); @@ -131,6 +132,11 @@ p.form-error { padding: .5em 1em; } +.form-error>h4 { + margin: 0; + margin-top: .5em; +} + .split { display: flex; flex-direction: row; @@ -164,6 +170,10 @@ div.dual-fields { margin-left: 1em; } +p.heading { + margin-top: 0; +} + .raised { background-color: var(--clr-surface-tonal-a10); padding: 1em; diff --git a/src/main.rs b/src/main.rs index 58e8170..d0dda16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ fn rocket() -> _ { public::index, public::show_profile, form::register_tag::show_register, - form::register_tag::handle_register + form::register_tag::handle_register, ], ) } diff --git a/src/routes/form/register_tag.rs b/src/routes/form/register_tag.rs index c2b4494..16f72cc 100644 --- a/src/routes/form/register_tag.rs +++ b/src/routes/form/register_tag.rs @@ -20,7 +20,8 @@ pub async fn show_register(db: DollTagsDb) -> PageResult { Ok(Template::render( "register", context! { - ids + ids, + previous: form::Context::default(), }, ) .into()) @@ -33,27 +34,27 @@ pub struct TagForm<'a> { #[field(validate=len(..32))] pub microchip_id: &'a str, - #[field(validate=len(..256))] + #[field(validate=len(1..=256))] pub name: &'a str, - #[field(validate=len(..32))] + #[field(validate=len(1..=32))] pub pronoun_subject: &'a str, - #[field(validate=len(..32))] + #[field(validate=len(1..=32))] pub pronoun_object: &'a str, - #[field(validate=len(..32))] + #[field(validate=len(1..=32))] pub pronoun_possessive: &'a str, - #[field(validate=len(..256))] + #[field(validate=len(1..=256))] pub handler_name: &'a str, - #[field(validate=len(..2048))] + #[field(validate=len(..=2048))] pub handler_link: &'a str, - #[field(validate=len(..256))] + #[field(validate=len(..=256))] pub kind: &'a str, - #[field(validate=len(..256))] + #[field(validate=len(..=256))] pub breed: &'a str, - #[field(validate=len(..256))] + #[field(validate=len(..=256))] pub behaviour: &'a str, - #[field(validate=len(..2048))] + #[field(validate=len(..=2048))] pub description: &'a str, #[field(validate=validate_chassis(self.chassis_id, self.chassis_color, "chassis_type"))] @@ -75,11 +76,11 @@ fn validate_id<'v>(id: &str) -> form::Result<'v, ()> { fn validate_chassis<'v>(a: &str, b: &str, c: &str, field: &str) -> form::Result<'v, ()> { let all_empty = a.len() == 0 && b.len() == 0 && c.len() == 0; let all_full = a.len() != 0 - && a.len() < 256 + && a.len() <= 256 && b.len() != 0 - && b.len() < 256 + && b.len() <= 256 && c.len() != 0 - && c.len() < 256; + && c.len() <= 256; if !all_empty && !all_full { Err(form::Error::validation(format!( @@ -92,7 +93,29 @@ fn validate_chassis<'v>(a: &str, b: &str, c: &str, field: &str) -> form::Result< } #[post("/register", data = "")] -pub async fn handle_register(mut db: DollTagsDb, tag: Form>) -> PageResult { +pub async fn handle_register( + mut db: DollTagsDb, + tag: Form>>, +) -> PageResult { + let tag = match tag.value { + Some(ref values) => values, + None => { + // in case the form validation fails, this will be tasked with rendering the page again with submitted values and display errors + let ids = pick_ids(db).await?; + + println!("{:?}", &tag.context); + + return Ok(Template::render( + "register", + context! { + ids, + previous: &tag.context, + }, + ) + .into()); + } + }; + println!("register: {:?}", tag); fn normalize_opt(opt: &str) -> Option<&str> { if opt.len() != 0 { @@ -136,21 +159,3 @@ pub async fn handle_register(mut db: DollTagsDb, tag: Form>) -> Page Ok(Redirect::to(uri!(public::show_profile(Some(tag.ident), microchip_id))).into()) } - -#[post("/register", data = "", rank = 2)] -pub async fn handle_retry_register( - db: DollTagsDb, - tag: Form>>, -) -> PageResult { - // in case the form validation fails, this will be tasked with rendering the page again with submitted values and display errors - let ids = pick_ids(db).await?; - - Ok(Template::render( - "register", - context! { - ids, - previous: &tag.context, - }, - ) - .into()) -} diff --git a/templates/macros.html.tera b/templates/macros.html.tera index 40b7bf5..e69de29 100644 --- a/templates/macros.html.tera +++ b/templates/macros.html.tera @@ -1,7 +0,0 @@ -{% macro pretty_pronouns(pronouns) %} -{% set fragments = pronouns | split(pat="/") %} -{% for fr in fragments %} -{{fr}} -{% if not loop.last %}/{% endif %} -{% endfor %} -{% endmacro pretty_pronouns %} \ No newline at end of file diff --git a/templates/macros/display.html.tera b/templates/macros/display.html.tera new file mode 100644 index 0000000..40b7bf5 --- /dev/null +++ b/templates/macros/display.html.tera @@ -0,0 +1,7 @@ +{% macro pretty_pronouns(pronouns) %} +{% set fragments = pronouns | split(pat="/") %} +{% for fr in fragments %} +{{fr}} +{% if not loop.last %}/{% endif %} +{% endfor %} +{% endmacro pretty_pronouns %} \ No newline at end of file diff --git a/templates/macros/form.html.tera b/templates/macros/form.html.tera new file mode 100644 index 0000000..0d1652f --- /dev/null +++ b/templates/macros/form.html.tera @@ -0,0 +1,6 @@ +{% macro value(ctx, name) %} +{% set values = ctx.values | get(key=name, default=[]) %} +{% if values | length > 0 %} +value="{{values | first}}" +{% endif %} +{% endmacro value %} \ No newline at end of file diff --git a/templates/register.html.tera b/templates/register.html.tera index bc30cd0..8f9754a 100644 --- a/templates/register.html.tera +++ b/templates/register.html.tera @@ -1,4 +1,5 @@ {% extends "base" %} +{% import "macros/form" as form %} {% block title %}Register a new tag - {% endblock title %} {% block main %}

Register a new tag

@@ -16,6 +17,18 @@

Now, to the registration!

+ {% if previous.form_errors | length > 0 %} +
+

Some errors were encountered...

+ +
    + {% for err in previous.form_errors %} +
  • {{err}}
  • + {% endfor %} +
+
+ {% endif %} +

Firstly, the tag's ID

@@ -26,9 +39,9 @@
-

+

+ placeholder="000000" {{form::value(ctx=previous, name="ident" )}} />

Or pick one of those pre-generated ones

@@ -38,9 +51,10 @@
-

+

+ placeholder="A hexadecimal ID" {{form::value(ctx=previous, name="microchip_id" )}}>
@@ -53,16 +67,17 @@
- +

Remember: names only mean what you want them to!

/ + placeholder="they" {{form::value(ctx=previous, name="pronoun_subject" )}} /> / / + placeholder="them" {{form::value(ctx=previous, name="pronoun_object" )}} /> / + placeholder="theirs" {{form::value(ctx=previous, name="pronoun_possessive" )}} />
@@ -70,12 +85,14 @@
- +
+ placeholder="E-mails can be entered using mailto:" {{form::value(ctx=previous, + name="handler_link" )}} />

Optional, will make the handler's name clickable.

@@ -95,21 +112,25 @@
- +
- +
- +
+ placeholder="feel free to write a bit about it or not at all" {{form::value(ctx=previous, + name="description" )}}>
@@ -126,15 +147,18 @@
- +
- +
- +
diff --git a/templates/show_profile.html.tera b/templates/show_profile.html.tera index c62ada8..d78cbcd 100644 --- a/templates/show_profile.html.tera +++ b/templates/show_profile.html.tera @@ -1,5 +1,5 @@ {% extends "base" %} -{% import "macros" as macros %} +{% import "macros/display" as macros %} {% block title %}{{ profile.name }} - {% endblock title %} {% block main %}