From 3775d3a9c9b1fd2921c2f0010c63c0c6f585e1ab Mon Sep 17 00:00:00 2001 From: Bat Date: Sat, 7 Jul 2018 22:51:48 +0200 Subject: [PATCH] HTML validation + Actually associate messages to errors + Fix inverted behavior on new blog and post form --- src/routes/blogs.rs | 13 +++++++++---- src/routes/comments.rs | 2 +- src/routes/posts.rs | 12 ++++++++---- src/routes/session.rs | 4 ++-- src/routes/user.rs | 2 +- templates/blogs/new.html.tera | 2 +- templates/macros.html.tera | 6 +++--- templates/posts/new.html.tera | 6 +++--- templates/session/login.html.tera | 5 +++-- templates/users/new.html.tera | 6 +++--- 10 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index f7dd94a2..9a6fcb8c 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -5,6 +5,7 @@ use rocket::{ }; use rocket_contrib::Template; use serde_json; +use std::{collections::HashMap, borrow::Cow}; use validator::{Validate, ValidationError, ValidationErrors}; use plume_common::activity_pub::ActivityStream; @@ -54,7 +55,7 @@ fn new_auth() -> Flash{ #[derive(FromForm, Validate, Serialize)] struct NewBlogForm { - #[validate(custom = "valid_slug")] + #[validate(custom(function = "valid_slug", message = "Invalid name"))] pub title: String } @@ -71,14 +72,17 @@ fn valid_slug(title: &str) -> Result<(), ValidationError> { fn create(conn: DbConn, data: LenientForm, user: User) -> Result { let form = data.get(); let slug = utils::make_actor_id(form.title.to_string()); - let slug_taken_err = Blog::find_local(&*conn, slug.clone()).ok_or(ValidationError::new("existing_slug")); let mut errors = match form.validate() { Ok(_) => ValidationErrors::new(), Err(e) => e }; - if let Err(e) = slug_taken_err { - errors.add("title", e) + if let Some(_) = Blog::find_local(&*conn, slug.clone()) { + errors.add("title", ValidationError { + code: Cow::from("existing_slug"), + message: Some(Cow::from("A blog with the same name already exists.")), + params: HashMap::new() + }); } if errors.is_empty() { @@ -98,6 +102,7 @@ fn create(conn: DbConn, data: LenientForm, user: User) -> Result, - #[validate(length(min = "1"))] + #[validate(length(min = "1", message = "Your comment can't be empty"))] pub content: String } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 182e101f..30051042 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -4,6 +4,7 @@ use rocket::request::LenientForm; use rocket::response::{Redirect, Flash}; use rocket_contrib::Template; use serde_json; +use std::{collections::HashMap, borrow::Cow}; use validator::{Validate, ValidationError, ValidationErrors}; use plume_common::activity_pub::{broadcast, ActivityStream}; @@ -86,7 +87,7 @@ fn new(blog: String, user: User, conn: DbConn) -> Template { #[derive(FromForm, Validate, Serialize)] struct NewPostForm { - #[validate(custom = "valid_slug")] + #[validate(custom(function = "valid_slug", message = "Invalid title"))] pub title: String, pub content: String, pub license: String @@ -108,14 +109,17 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap(); let form = data.get(); let slug = form.title.to_string().to_kebab_case(); - let slug_taken_err = Blog::find_local(&*conn, slug.clone()).ok_or(ValidationError::new("existing_slug")); let mut errors = match form.validate() { Ok(_) => ValidationErrors::new(), Err(e) => e }; - if let Err(e) = slug_taken_err { - errors.add("title", e); + if let Some(_) = Post::find_by_slug(&*conn, slug.clone(), blog.id) { + errors.add("title", ValidationError { + code: Cow::from("existing_slug"), + message: Some(Cow::from("A post with the same title already exists.")), + params: HashMap::new() + }); } if errors.is_empty() { diff --git a/src/routes/session.rs b/src/routes/session.rs index 655fc693..fd79f057 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -38,9 +38,9 @@ fn new_message(user: Option, message: Message) -> Template { #[derive(FromForm, Validate, Serialize)] struct LoginForm { - #[validate(length(min = "1"))] + #[validate(length(min = "1", message = "We need an email or a username to identify you"))] email_or_name: String, - #[validate(length(min = "8"))] + #[validate(length(min = "8", message = "Your password should be at least 8 characters long"))] password: String } diff --git a/src/routes/user.rs b/src/routes/user.rs index f736901e..1b0f42e9 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -161,7 +161,7 @@ fn update(_name: String, conn: DbConn, user: User, data: LenientForm{{ "Create a blog" | _ }}
- {{ macros::input(name="title", label="Title", errors=errors, form=form) }} + {{ macros::input(name="title", label="Title", errors=errors, form=form, props='required minlength="1"') }}
diff --git a/templates/macros.html.tera b/templates/macros.html.tera index dc6187f9..868ee781 100644 --- a/templates/macros.html.tera +++ b/templates/macros.html.tera @@ -21,12 +21,12 @@

{% endmacro post_card %} -{% macro input(name, label, errors, form, type="text") %} +{% macro input(name, label, errors, form, type="text", props="") %} {% if errors is defined and errors[name] %} {% for err in errors[name] %} -

{{ err.message | _ }}

+

{{ err.message | default(value="Unknown error") }}

{% endfor %} {% endif %} - + {% endmacro input %} diff --git a/templates/posts/new.html.tera b/templates/posts/new.html.tera index 0878658b..335e2396 100644 --- a/templates/posts/new.html.tera +++ b/templates/posts/new.html.tera @@ -1,4 +1,5 @@ {% extends "base" %} +{% import "macros" as macros %} {% block title %} {{ "New post" | _ }} @@ -9,14 +10,13 @@
{{ macros::input(name="title", label="Title", errors=errors, form=form) }} - {% if errors is defined and errors.content %} {% for err in errors.content %} -

{{ err.message | _ }}

+

{{ err.message | default(value="Unknown error") | _ }}

{% endfor %} {% endif %} - + {{ macros::input(name="license", label="License", errors=errors, form=form) }} diff --git a/templates/session/login.html.tera b/templates/session/login.html.tera index 4a0ac027..bbee8c98 100644 --- a/templates/session/login.html.tera +++ b/templates/session/login.html.tera @@ -1,4 +1,5 @@ {% extends "base" %} +{% import "macros" as macros %} {% block title %} {{ "Login" | _ }} @@ -10,8 +11,8 @@

{{ message }}

{% endif %} - {{ macros::input(name="email_or_name", label="Username or email", errors=errors, form=form) }} - {{ macros::input(name="password", label="Password", errors=errors, form=form, type="password") }} + {{ macros::input(name="email_or_name", label="Username or email", errors=errors, form=form, props='minlenght="1"') }} + {{ macros::input(name="password", label="Password", errors=errors, form=form, type="password", props='minlenght="8"') }}
diff --git a/templates/users/new.html.tera b/templates/users/new.html.tera index 462d8a81..b0aef4e1 100644 --- a/templates/users/new.html.tera +++ b/templates/users/new.html.tera @@ -8,10 +8,10 @@ {% block content %}

{{ "Create an account" | _ }}

- {{ macros::input(name="username", label="Username", errors=errors, form=form) }} + {{ macros::input(name="username", label="Username", errors=errors, form=form, props='minlenght="1"') }} {{ macros::input(name="email", label="Email", errors=errors, form=form, type="email") }} - {{ macros::input(name="password", label="Password", errors=errors, form=form, type="password") }} - {{ macros::input(name="password_confirmation", label="Password confirmation", errors=errors, form=form, type="password") }} + {{ macros::input(name="password", label="Password", errors=errors, form=form, type="password", props='minlenght="8"') }} + {{ macros::input(name="password_confirmation", label="Password confirmation", errors=errors, form=form, type="password", props='minlenght="8"') }}