diff --git a/PostText.pl b/PostText.pl index ea2c361..524e857 100755 --- a/PostText.pl +++ b/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(); diff --git a/README.md b/README.md index aba5800..f386ebf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/t/post.t b/t/post.t index 8ab4305..ce3bae5 100644 --- a/t/post.t +++ b/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/); diff --git a/templates/post.html.ep b/templates/post.html.ep index 2c2f75a..aef4b41 100644 --- a/templates/post.html.ep +++ b/templates/post.html.ep @@ -5,18 +5,28 @@
<%= label_for name => 'Author' %> <%= text_field name =>'Anonymous', maxlength => 63, minlength => 1 %> + <% if (my $error = validation->error('name')) { =%> +

Invalid name: 1 to 63 characters please.

+ <% } =%>
<%= label_for title => 'Title' %> <%= text_field 'title', maxlength => 127, minlength => 1 %> + <% if (my $error = validation->error('title')) { =%> +

Invalid title: 1 to 127 characters please.

+ <% } =%>
<%= 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')) { =%> +

Invalid post: Up to 4,000 characters only.

+ <% } =%>
<%= submit_button 'Post', class => 'post button' %>