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" | _ }}
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 @@
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" | _ }}