Handle invalid input a lil better
This commit is contained in:
parent
25d8085529
commit
4adc4b18b0
10
PostText.pl
10
PostText.pl
|
@ -28,8 +28,6 @@ helper thread => sub {
|
||||||
under sub ($c) {
|
under sub ($c) {
|
||||||
$c->session(expires => time() + 31536000);
|
$c->session(expires => time() + 31536000);
|
||||||
|
|
||||||
$c->stash(status => 400) if $c->flash('invalid_input');
|
|
||||||
|
|
||||||
1;
|
1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -73,17 +71,17 @@ any [qw{GET POST}], '/post', sub ($c) {
|
||||||
$v->required('post' )->size(2, 4000);
|
$v->required('post' )->size(2, 4000);
|
||||||
|
|
||||||
if ($v->has_error) {
|
if ($v->has_error) {
|
||||||
$c->flash(invalid_input => 'Invalid thread title/text.')
|
$c->stash(status => 400)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$c->thread->create_thread(
|
$c->thread->create_thread(
|
||||||
$thread_author,
|
$thread_author,
|
||||||
$thread_title,
|
$thread_title,
|
||||||
$thread_body
|
$thread_body
|
||||||
)
|
);
|
||||||
}
|
|
||||||
|
|
||||||
return $c->redirect_to('view');
|
return $c->redirect_to('view');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $c->render();
|
return $c->render();
|
||||||
|
|
|
@ -24,6 +24,6 @@ Run the tests locally (against development environment)
|
||||||
|
|
||||||
## TODOs
|
## TODOs
|
||||||
|
|
||||||
1. Display error for invalid input (call `flash()` in template)
|
1. Setup [Mojolicious::Plugin::AssetPack](https://metacpan.org/pod/Mojolicious::Plugin::AssetPack)
|
||||||
1. Pick a date format
|
1. Pick a date format
|
||||||
1. Reply model
|
1. Reply model
|
||||||
|
|
21
t/post.t
21
t/post.t
|
@ -6,19 +6,25 @@ use Test::More;
|
||||||
use Mojo::File qw{curfile};
|
use Mojo::File qw{curfile};
|
||||||
use Test::Mojo;
|
use Test::Mojo;
|
||||||
|
|
||||||
my $script = curfile->dirname->sibling('PostText.pl');
|
my $script = curfile->dirname->sibling('PostText.pl');
|
||||||
my $t = Test::Mojo->new($script);
|
my $t = Test::Mojo->new($script);
|
||||||
my %valid_params = (
|
my %valid_params = (
|
||||||
name => 'Anonymous',
|
name => 'Anonymous',
|
||||||
title => 'hi',
|
title => 'hi',
|
||||||
post => 'ayy... lmao'
|
post => 'ayy... lmao'
|
||||||
);
|
);
|
||||||
my %invalid_params = (
|
my %invalid_title = (
|
||||||
name => 'Anonymous',
|
name => 'Anonymous',
|
||||||
title => '',
|
title => '',
|
||||||
|
post => 'ayy... lmao'
|
||||||
|
);
|
||||||
|
my %invalid_post = (
|
||||||
|
name => 'Anonymous',
|
||||||
|
title => 'hi',
|
||||||
post => 'a'
|
post => 'a'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
$t->ua->max_redirects(1);
|
$t->ua->max_redirects(1);
|
||||||
|
|
||||||
# GET
|
# GET
|
||||||
|
@ -27,8 +33,11 @@ $t->get_ok('/post')->status_is(200)->text_like(h2 => qr/New Thread/);
|
||||||
# POST
|
# POST
|
||||||
$t->post_ok('/post')->status_is(200)->text_like(h2 => qr/New Thread/);
|
$t->post_ok('/post')->status_is(200)->text_like(h2 => qr/New Thread/);
|
||||||
|
|
||||||
$t->post_ok('/post', form => \%invalid_params)->status_is(400)
|
$t->post_ok('/post', form => \%invalid_title)->status_is(400)
|
||||||
->text_like(h2 => qr/View Threads/);
|
->text_like(p => qr/Invalid title/);
|
||||||
|
|
||||||
|
$t->post_ok('/post', form => \%invalid_post)->status_is(400)
|
||||||
|
->text_like(p => qr/Invalid post/);
|
||||||
|
|
||||||
$t->post_ok('/post', form => \%valid_params)->status_is(200)
|
$t->post_ok('/post', form => \%valid_params)->status_is(200)
|
||||||
->text_like(h2 => qr/View Threads/);
|
->text_like(h2 => qr/View Threads/);
|
||||||
|
|
|
@ -5,18 +5,28 @@
|
||||||
<div class="name field">
|
<div class="name field">
|
||||||
<%= label_for name => 'Author' %>
|
<%= label_for name => 'Author' %>
|
||||||
<%= text_field name =>'Anonymous', maxlength => 63, minlength => 1 %>
|
<%= text_field name =>'Anonymous', maxlength => 63, minlength => 1 %>
|
||||||
|
<% if (my $error = validation->error('name')) { =%>
|
||||||
|
<p class="field-with-error">Invalid name: 1 to 63 characters please.</p>
|
||||||
|
<% } =%>
|
||||||
</div>
|
</div>
|
||||||
<div class="title field">
|
<div class="title field">
|
||||||
<%= label_for title => 'Title' %>
|
<%= label_for title => 'Title' %>
|
||||||
<%= text_field 'title', maxlength => 127, minlength => 1 %>
|
<%= text_field 'title', maxlength => 127, minlength => 1 %>
|
||||||
|
<% if (my $error = validation->error('title')) { =%>
|
||||||
|
<p class="field-with-error">Invalid title: 1 to 127 characters please.</p>
|
||||||
|
<% } =%>
|
||||||
</div>
|
</div>
|
||||||
<div class="text field">
|
<div class="text field">
|
||||||
<%= label_for post => 'Text' %>
|
<%= label_for post => 'Text' %>
|
||||||
<%= text_area 'post',
|
<%= text_area 'post', (
|
||||||
maxlength => 4000,
|
maxlength => 4000,
|
||||||
minlength => 2,
|
minlength => 2,
|
||||||
required => 'true',
|
required => 'true',
|
||||||
rows => 6 %>
|
rows => 6
|
||||||
|
) %>
|
||||||
|
<% if (my $error = validation->error('post')) { =%>
|
||||||
|
<p class="field-with-error">Invalid post: Up to 4,000 characters only.</p>
|
||||||
|
<% } =%>
|
||||||
</div>
|
</div>
|
||||||
<%= submit_button 'Post', class => 'post button' %>
|
<%= submit_button 'Post', class => 'post button' %>
|
||||||
</form>
|
</form>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user