HTML validation + Actually associate messages to errors + Fix inverted behavior on new blog and post form

This commit is contained in:
Bat 2018-07-07 22:51:48 +02:00
parent e5c1b3259d
commit 3775d3a9c9
10 changed files with 34 additions and 24 deletions

View File

@ -5,6 +5,7 @@ use rocket::{
}; };
use rocket_contrib::Template; use rocket_contrib::Template;
use serde_json; use serde_json;
use std::{collections::HashMap, borrow::Cow};
use validator::{Validate, ValidationError, ValidationErrors}; use validator::{Validate, ValidationError, ValidationErrors};
use plume_common::activity_pub::ActivityStream; use plume_common::activity_pub::ActivityStream;
@ -54,7 +55,7 @@ fn new_auth() -> Flash<Redirect>{
#[derive(FromForm, Validate, Serialize)] #[derive(FromForm, Validate, Serialize)]
struct NewBlogForm { struct NewBlogForm {
#[validate(custom = "valid_slug")] #[validate(custom(function = "valid_slug", message = "Invalid name"))]
pub title: String pub title: String
} }
@ -71,14 +72,17 @@ fn valid_slug(title: &str) -> Result<(), ValidationError> {
fn create(conn: DbConn, data: LenientForm<NewBlogForm>, user: User) -> Result<Redirect, Template> { fn create(conn: DbConn, data: LenientForm<NewBlogForm>, user: User) -> Result<Redirect, Template> {
let form = data.get(); let form = data.get();
let slug = utils::make_actor_id(form.title.to_string()); 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() { let mut errors = match form.validate() {
Ok(_) => ValidationErrors::new(), Ok(_) => ValidationErrors::new(),
Err(e) => e Err(e) => e
}; };
if let Err(e) = slug_taken_err { if let Some(_) = Blog::find_local(&*conn, slug.clone()) {
errors.add("title", e) 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() { if errors.is_empty() {
@ -98,6 +102,7 @@ fn create(conn: DbConn, data: LenientForm<NewBlogForm>, user: User) -> Result<Re
Ok(Redirect::to(uri!(details: name = slug.clone()))) Ok(Redirect::to(uri!(details: name = slug.clone())))
} else { } else {
println!("{:?}", errors);
Err(Template::render("blogs/new", json!({ Err(Template::render("blogs/new", json!({
"account": user, "account": user,
"errors": errors.inner(), "errors": errors.inner(),

View File

@ -20,7 +20,7 @@ use inbox::Inbox;
#[derive(FromForm, Debug, Validate)] #[derive(FromForm, Debug, Validate)]
struct NewCommentForm { struct NewCommentForm {
pub responding_to: Option<i32>, pub responding_to: Option<i32>,
#[validate(length(min = "1"))] #[validate(length(min = "1", message = "Your comment can't be empty"))]
pub content: String pub content: String
} }

View File

@ -4,6 +4,7 @@ use rocket::request::LenientForm;
use rocket::response::{Redirect, Flash}; use rocket::response::{Redirect, Flash};
use rocket_contrib::Template; use rocket_contrib::Template;
use serde_json; use serde_json;
use std::{collections::HashMap, borrow::Cow};
use validator::{Validate, ValidationError, ValidationErrors}; use validator::{Validate, ValidationError, ValidationErrors};
use plume_common::activity_pub::{broadcast, ActivityStream}; use plume_common::activity_pub::{broadcast, ActivityStream};
@ -86,7 +87,7 @@ fn new(blog: String, user: User, conn: DbConn) -> Template {
#[derive(FromForm, Validate, Serialize)] #[derive(FromForm, Validate, Serialize)]
struct NewPostForm { struct NewPostForm {
#[validate(custom = "valid_slug")] #[validate(custom(function = "valid_slug", message = "Invalid title"))]
pub title: String, pub title: String,
pub content: String, pub content: String,
pub license: String pub license: String
@ -108,14 +109,17 @@ fn create(blog_name: String, data: LenientForm<NewPostForm>, user: User, conn: D
let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap(); let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap();
let form = data.get(); let form = data.get();
let slug = form.title.to_string().to_kebab_case(); 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() { let mut errors = match form.validate() {
Ok(_) => ValidationErrors::new(), Ok(_) => ValidationErrors::new(),
Err(e) => e Err(e) => e
}; };
if let Err(e) = slug_taken_err { if let Some(_) = Post::find_by_slug(&*conn, slug.clone(), blog.id) {
errors.add("title", e); 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() { if errors.is_empty() {

View File

@ -38,9 +38,9 @@ fn new_message(user: Option<User>, message: Message) -> Template {
#[derive(FromForm, Validate, Serialize)] #[derive(FromForm, Validate, Serialize)]
struct LoginForm { 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, email_or_name: String,
#[validate(length(min = "8"))] #[validate(length(min = "8", message = "Your password should be at least 8 characters long"))]
password: String password: String
} }

View File

@ -161,7 +161,7 @@ fn update(_name: String, conn: DbConn, user: User, data: LenientForm<UpdateUserF
} }
#[derive(FromForm, Serialize, Validate)] #[derive(FromForm, Serialize, Validate)]
#[validate(schema(function = "passwords_match", skip_on_field_errors = "false"))] #[validate(schema(function = "passwords_match", skip_on_field_errors = "false", message = "Passwords are not matching"))]
struct NewUserForm { struct NewUserForm {
#[validate(length(min = "1", message = "Username can't be empty"))] #[validate(length(min = "1", message = "Username can't be empty"))]
username: String, username: String,

View File

@ -8,7 +8,7 @@
{% block content %} {% block content %}
<h1>{{ "Create a blog" | _ }}</h1> <h1>{{ "Create a blog" | _ }}</h1>
<form method="post"> <form method="post">
{{ macros::input(name="title", label="Title", errors=errors, form=form) }} {{ macros::input(name="title", label="Title", errors=errors, form=form, props='required minlength="1"') }}
<input type="submit" value="{{ "Create blog" | _ }}"/> <input type="submit" value="{{ "Create blog" | _ }}"/>
</form> </form>

View File

@ -21,12 +21,12 @@
</p> </p>
</div> </div>
{% endmacro post_card %} {% endmacro post_card %}
{% macro input(name, label, errors, form, type="text") %} {% macro input(name, label, errors, form, type="text", props="") %}
<label for="{{ name }}">{{ label | _ }}</label> <label for="{{ name }}">{{ label | _ }}</label>
{% if errors is defined and errors[name] %} {% if errors is defined and errors[name] %}
{% for err in errors[name] %} {% for err in errors[name] %}
<p class="error">{{ err.message | _ }}</p> <p class="error">{{ err.message | default(value="Unknown error") }}</p>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<input type="{{ type }}" id="{{ name }}" name="{{ name }}" value="{{ form[name] | default(value="") }}"/> <input type="{{ type }}" id="{{ name }}" name="{{ name }}" value="{{ form[name] | default(value="") }}" {{ props | safe }}/>
{% endmacro input %} {% endmacro input %}

View File

@ -1,4 +1,5 @@
{% extends "base" %} {% extends "base" %}
{% import "macros" as macros %}
{% block title %} {% block title %}
{{ "New post" | _ }} {{ "New post" | _ }}
@ -9,14 +10,13 @@
<form class="new-post" method="post"> <form class="new-post" method="post">
{{ macros::input(name="title", label="Title", errors=errors, form=form) }} {{ macros::input(name="title", label="Title", errors=errors, form=form) }}
<label for="{{ name }}">{{ label | _ }}</label>
{% if errors is defined and errors.content %} {% if errors is defined and errors.content %}
{% for err in errors.content %} {% for err in errors.content %}
<p class="error">{{ err.message | _ }}</p> <p class="error">{{ err.message | default(value="Unknown error") | _ }}</p>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<textarea name="content" placeholder="{{ "Content" | _ }}" value="{{ form.content | default(value="") }}"></textarea> <textarea id="content" name="content" placeholder="{{ "Content" | _ }}" value="{{ form.content | default(value="") }}"></textarea>
{{ macros::input(name="license", label="License", errors=errors, form=form) }} {{ macros::input(name="license", label="License", errors=errors, form=form) }}

View File

@ -1,4 +1,5 @@
{% extends "base" %} {% extends "base" %}
{% import "macros" as macros %}
{% block title %} {% block title %}
{{ "Login" | _ }} {{ "Login" | _ }}
@ -10,8 +11,8 @@
<p>{{ message }}</p> <p>{{ message }}</p>
{% endif %} {% endif %}
<form method="post"> <form method="post">
{{ macros::input(name="email_or_name", label="Username or email", errors=errors, form=form) }} {{ 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") }} {{ macros::input(name="password", label="Password", errors=errors, form=form, type="password", props='minlenght="8"') }}
<input type="submit" value="{{ "Login" | _ }}" /> <input type="submit" value="{{ "Login" | _ }}" />
</form> </form>

View File

@ -8,10 +8,10 @@
{% block content %} {% block content %}
<h1>{{ "Create an account" | _ }}</h1> <h1>{{ "Create an account" | _ }}</h1>
<form method="post"> <form method="post">
{{ 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="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", 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") }} {{ macros::input(name="password_confirmation", label="Password confirmation", errors=errors, form=form, type="password", props='minlenght="8"') }}
<input type="submit" value="{{ "Create account" | _ }}" /> <input type="submit" value="{{ "Create account" | _ }}" />
</form> </form>