From 5f3afe900f303a558197a3be278223a329610c4f Mon Sep 17 00:00:00 2001
From: Bat
Date: Fri, 6 Jul 2018 19:29:36 +0200
Subject: [PATCH] Display errors on invalid forms
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
It will probably need a bit of styling…
---
Cargo.lock | 2 ++
Cargo.toml | 2 ++
src/main.rs | 3 +++
src/routes/blogs.rs | 9 ++++++---
src/routes/posts.rs | 11 +++++++----
src/routes/session.rs | 11 ++++++++---
src/routes/user.rs | 17 ++++++++++-------
templates/blogs/new.html.tera | 5 +++--
templates/macros.html.tera | 9 +++++++++
templates/posts/new.html.tera | 15 +++++++++++----
templates/session/login.html.tera | 7 ++-----
templates/users/new.html.tera | 16 +++++-----------
12 files changed, 68 insertions(+), 39 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index 3e41543..3304acc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -999,6 +999,8 @@ dependencies = [
"rocket_csrf 0.1.0 (git+https://github.com/fdb-hiroshima/rocket_csrf?rev=80687a64a8b9d44e4983e63cca6d707498e92fc7)",
"rocket_i18n 0.1.1 (git+https://github.com/BaptisteGelez/rocket_i18n?rev=5b4225d5bed5769482dc926a7e6d6b79f1217be6)",
"rpassword 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.43 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"validator 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"validator_derive 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 265c472..a848910 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,6 +10,8 @@ failure = "0.1"
gettext-rs = "0.4"
heck = "0.3.0"
rpassword = "2.0"
+serde = "1.0"
+serde_derive = "1.0"
serde_json = "1.0"
validator = "0.7"
validator_derive = "0.7"
diff --git a/src/main.rs b/src/main.rs
index bdf1387..215e210 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,6 +15,9 @@ extern crate rocket_contrib;
extern crate rocket_csrf;
extern crate rocket_i18n;
extern crate rpassword;
+extern crate serde;
+#[macro_use]
+extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate validator;
diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs
index e996e6c..f7dd94a 100644
--- a/src/routes/blogs.rs
+++ b/src/routes/blogs.rs
@@ -41,7 +41,9 @@ fn activity_details(name: String, conn: DbConn) -> ActivityStream {
#[get("/blogs/new")]
fn new(user: User) -> Template {
Template::render("blogs/new", json!({
- "account": user
+ "account": user,
+ "errors": null,
+ "form": null
}))
}
@@ -50,7 +52,7 @@ fn new_auth() -> Flash{
utils::requires_login("You need to be logged in order to create a new blog", uri!(new))
}
-#[derive(FromForm, Validate)]
+#[derive(FromForm, Validate, Serialize)]
struct NewBlogForm {
#[validate(custom = "valid_slug")]
pub title: String
@@ -98,7 +100,8 @@ fn create(conn: DbConn, data: LenientForm, user: User) -> Result Template {
}))
} else {
Template::render("posts/new", json!({
- "account": user
+ "account": user,
+ "errors": null,
+ "form": null
}))
}
}
-#[derive(FromForm, Validate)]
+#[derive(FromForm, Validate, Serialize)]
struct NewPostForm {
#[validate(custom = "valid_slug")]
pub title: String,
@@ -113,7 +115,7 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D
Err(e) => e
};
if let Err(e) = slug_taken_err {
- errors.add("title", e)
+ errors.add("title", e);
}
if errors.is_empty() {
@@ -150,7 +152,8 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D
} else {
Err(Template::render("posts/new", json!({
"account": user,
- "errors": errors.inner()
+ "errors": errors.inner(),
+ "form": form
})))
}
}
diff --git a/src/routes/session.rs b/src/routes/session.rs
index 4f5062a..e948d8e 100644
--- a/src/routes/session.rs
+++ b/src/routes/session.rs
@@ -14,7 +14,9 @@ use plume_models::{
#[get("/login")]
fn new(user: Option) -> Template {
Template::render("session/login", json!({
- "account": user
+ "account": user,
+ "errors": null,
+ "form": null
}))
}
@@ -27,7 +29,9 @@ struct Message {
fn new_message(user: Option, message: Message) -> Template {
Template::render("session/login", json!({
"account": user,
- "message": message.m
+ "message": message.m,
+ "errors": null,
+ "form": null
}))
}
@@ -66,7 +70,8 @@ fn create(conn: DbConn, data: LenientForm, flash: Option ActivityStream
#[get("/users/new")]
fn new(user: Option) -> Template {
Template::render("users/new", json!({
- "account": user
+ "account": user,
+ "errors": null,
+ "form": null
}))
}
@@ -158,16 +160,16 @@ fn update(_name: String, conn: DbConn, user: User, data: LenientForm) -> Result{{ "Create a blog" | _ }}
{% endblock content %}
diff --git a/templates/macros.html.tera b/templates/macros.html.tera
index 6a10d1e..dc6187f 100644
--- a/templates/macros.html.tera
+++ b/templates/macros.html.tera
@@ -21,3 +21,12 @@
{% endmacro post_card %}
+{% macro input(name, label, errors, form, type="text") %}
+ {{ label | _ }}
+ {% if errors is defined and errors[name] %}
+ {% for err in errors[name] %}
+ {{ err.message | _ }}
+ {% endfor %}
+ {% endif %}
+
+{% endmacro input %}
diff --git a/templates/posts/new.html.tera b/templates/posts/new.html.tera
index 4cf457b..0878658 100644
--- a/templates/posts/new.html.tera
+++ b/templates/posts/new.html.tera
@@ -7,11 +7,18 @@
{% block content %}
{{ "Create a post" | _ }}
diff --git a/templates/session/login.html.tera b/templates/session/login.html.tera
index 4486bf3..4a0ac02 100644
--- a/templates/session/login.html.tera
+++ b/templates/session/login.html.tera
@@ -10,11 +10,8 @@
{{ message }}
{% endif %}
- {{ "Username or email" | _ }}
-
-
- {{ "Password" | _ }}
-
+ {{ 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") }}
diff --git a/templates/users/new.html.tera b/templates/users/new.html.tera
index edf328c..462d8a8 100644
--- a/templates/users/new.html.tera
+++ b/templates/users/new.html.tera
@@ -1,4 +1,5 @@
{% extends "base" %}
+{% import "macros" as macros %}
{% block title %}
{{ "New Account" | _ }}
@@ -7,17 +8,10 @@
{% block content %}
{{ "Create an account" | _ }}
- {{ "Username" | _ }}
-
-
- {{ "Email" | _ }}
-
-
- {{ "Password" | _ }}
-
-
- {{ "Password confirmation" | _ }}
-
+ {{ macros::input(name="username", label="Username", errors=errors, form=form) }}
+ {{ 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") }}