Skip to content

Commit

Permalink
added is_submitted method to Mojolicious::Validator::Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 27, 2013
1 parent 13e8ae1 commit 9bada75
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Mojolicious/Validator/Validation.pm
Expand Up @@ -49,6 +49,8 @@ sub has_errors {
$_[1] ? exists $_[0]{errors}{$_[1]} : !!keys %{$_[0]{errors}};
}

sub is_submitted { !!keys %{shift->input} }

sub is_valid { exists $_[0]->output->{$_[1] // $_[0]->topic} }

sub optional {
Expand Down Expand Up @@ -182,6 +184,12 @@ validation checks.
Check if validation resulted in errors, defaults to checking all fields.
=head2 is_submitted
my $success = $validation->is_submitted;
Check if data has been submitted for validation.
=head2 is_valid
my $success = $validation->is_valid;
Expand Down
5 changes: 5 additions & 0 deletions t/mojolicious/validation_lite_app.t
Expand Up @@ -12,6 +12,7 @@ use Test::Mojo;
get '/' => sub {
my $self = shift;
my $validation = $self->validation;
return $self->render unless $validation->is_submitted;
$validation->required('foo')->size(2, 5);
$validation->optional('bar')->size(2, 5);
$validation->optional('baz')->size(2, 5);
Expand Down Expand Up @@ -87,6 +88,10 @@ is_deeply [$validation->errors('yada')->each],
is $validation->topic, 'yada', 'right topic';
ok $validation->has_errors('bar'), 'has errors';

# No validation
$t->get_ok('/')->status_is(200)->element_exists('form > input')
->element_exists('form > textarea')->element_exists('form > select');

# Successful validation
$t->get_ok('/?foo=ok')->status_is(200)->element_exists('form > input')
->element_exists('form > textarea')->element_exists('form > select');
Expand Down

0 comments on commit 9bada75

Please sign in to comment.