Handle invalid input a lil better

This commit is contained in:
swaggboi 2022-08-15 17:48:58 -04:00
parent 25d8085529
commit 4adc4b18b0
4 changed files with 32 additions and 15 deletions

View File

@ -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,18 +71,18 @@ 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();
}; };

View File

@ -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

View File

@ -13,12 +13,18 @@ my %valid_params = (
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/);

View File

@ -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>