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) {
|
||||
$c->session(expires => time() + 31536000);
|
||||
|
||||
$c->stash(status => 400) if $c->flash('invalid_input');
|
||||
|
||||
1;
|
||||
};
|
||||
|
||||
|
@ -73,17 +71,17 @@ any [qw{GET POST}], '/post', sub ($c) {
|
|||
$v->required('post' )->size(2, 4000);
|
||||
|
||||
if ($v->has_error) {
|
||||
$c->flash(invalid_input => 'Invalid thread title/text.')
|
||||
$c->stash(status => 400)
|
||||
}
|
||||
else {
|
||||
$c->thread->create_thread(
|
||||
$thread_author,
|
||||
$thread_title,
|
||||
$thread_body
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
return $c->redirect_to('view');
|
||||
return $c->redirect_to('view');
|
||||
}
|
||||
}
|
||||
|
||||
return $c->render();
|
||||
|
|
|
@ -24,6 +24,6 @@ Run the tests locally (against development environment)
|
|||
|
||||
## 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. Reply model
|
||||
|
|
21
t/post.t
21
t/post.t
|
@ -6,19 +6,25 @@ use Test::More;
|
|||
use Mojo::File qw{curfile};
|
||||
use Test::Mojo;
|
||||
|
||||
my $script = curfile->dirname->sibling('PostText.pl');
|
||||
my $t = Test::Mojo->new($script);
|
||||
my %valid_params = (
|
||||
my $script = curfile->dirname->sibling('PostText.pl');
|
||||
my $t = Test::Mojo->new($script);
|
||||
my %valid_params = (
|
||||
name => 'Anonymous',
|
||||
title => 'hi',
|
||||
post => 'ayy... lmao'
|
||||
);
|
||||
my %invalid_params = (
|
||||
my %invalid_title = (
|
||||
name => 'Anonymous',
|
||||
title => '',
|
||||
post => 'ayy... lmao'
|
||||
);
|
||||
my %invalid_post = (
|
||||
name => 'Anonymous',
|
||||
title => 'hi',
|
||||
post => 'a'
|
||||
);
|
||||
|
||||
|
||||
$t->ua->max_redirects(1);
|
||||
|
||||
# GET
|
||||
|
@ -27,8 +33,11 @@ $t->get_ok('/post')->status_is(200)->text_like(h2 => qr/New Thread/);
|
|||
# POST
|
||||
$t->post_ok('/post')->status_is(200)->text_like(h2 => qr/New Thread/);
|
||||
|
||||
$t->post_ok('/post', form => \%invalid_params)->status_is(400)
|
||||
->text_like(h2 => qr/View Threads/);
|
||||
$t->post_ok('/post', form => \%invalid_title)->status_is(400)
|
||||
->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)
|
||||
->text_like(h2 => qr/View Threads/);
|
||||
|
|
|
@ -5,18 +5,28 @@
|
|||
<div class="name field">
|
||||
<%= label_for name => 'Author' %>
|
||||
<%= 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 class="title field">
|
||||
<%= label_for title => 'Title' %>
|
||||
<%= 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 class="text field">
|
||||
<%= label_for post => 'Text' %>
|
||||
<%= text_area 'post',
|
||||
<%= text_area 'post', (
|
||||
maxlength => 4000,
|
||||
minlength => 2,
|
||||
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>
|
||||
<%= submit_button 'Post', class => 'post button' %>
|
||||
</form>
|
||||
|
|
Loading…
Reference in New Issue
Block a user