Skip to content

Commit

Permalink
changed return values of validation checks in Mojolicious::Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 6, 2013
1 parent 30bd1e2 commit d2cce28
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.45 2013-10-06
- Changed return values of validation checks in Mojolicious::Validator.
- Fixed unquoted attribute selector bug in Mojo::DOM::CSS.

4.44 2013-10-04
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -690,15 +690,17 @@ L<Mojolicious::Validator/"CHECKS">.

=head2 Adding form validation checks

Validation checks only have to return true or false and can be registered with
L<Mojolicious::Validator/"add_check">.
Validation checks can be registered with L<Mojolicious::Validator/"add_check">
and return a false value if they were successful. A true value may be used to
pass along additional information which can then be retrieved with
L<Mojolicious::Validator::Validation/"error">.

use Mojolicious::Lite;

# Add "range" check
app->validator->add_check(range => sub {
my ($validation, $name, $value, $min, $max) = @_;
return $value >= $min && $value <= $max;
return $value < $min || $value > $max;
});

get '/' => 'form';
Expand Down Expand Up @@ -741,9 +743,6 @@ L<Mojolicious::Validator/"add_check">.
</html>
</html>

Details about failed validation checks can be retrieved with
L<Mojolicious::Validator::Validation/"error">.

=head2 Adding helpers

Adding and redefining helpers is very easy, you can use them to do pretty much
Expand Down
12 changes: 6 additions & 6 deletions lib/Mojolicious/Validator.pm
Expand Up @@ -19,22 +19,22 @@ sub validation {

sub _equal_to {
my ($validation, $name, $value, $to) = @_;
return undef unless defined(my $other = $validation->input->{$to});
return $value eq $other;
return 1 unless defined(my $other = $validation->input->{$to});
return $value ne $other;
}

sub _in {
my ($validation, $name, $value) = (shift, shift, shift);
$value eq $_ && return 1 for @_;
return undef;
$value eq $_ && return undef for @_;
return 1;
}

sub _like { $_[2] =~ $_[3] }
sub _like { $_[2] !~ $_[3] }

sub _size {
my ($validation, $name, $value, $min, $max) = @_;
my $len = length $value;
return $len >= $min && $len <= $max;
return !($len >= $min && $len <= $max);
}

1;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Validator/Validation.pm
Expand Up @@ -30,9 +30,9 @@ sub check {
my $name = $self->topic;
my $input = $self->input->{$name};
for my $value (ref $input eq 'ARRAY' ? @$input : $input) {
next if $self->$cb($name, $value, @_);
next unless my $result = $self->$cb($name, $value, @_);
delete $self->output->{$name};
$self->{error}{$name} = [$check, @_];
$self->{error}{$name} = [$check, $result, @_];
last;
}

Expand Down Expand Up @@ -152,7 +152,7 @@ Perform validation check.
Return details about failed validation check.
my ($check, @args) = @{$validation->error('foo')};
my ($check, $result, @args) = @{$validation->error('foo')};
=head2 has_data
Expand Down
23 changes: 13 additions & 10 deletions t/mojolicious/validation_lite_app.t
Expand Up @@ -10,7 +10,7 @@ use Mojolicious::Lite;
use Test::Mojo;

# Custom check
app->validator->add_check(two => sub { length $_[2] == 2 });
app->validator->add_check(two => sub { length $_[2] == 2 ? undef : 'ohoh' });

any '/' => sub {
my $self = shift;
Expand Down Expand Up @@ -58,12 +58,12 @@ ok !$validation->optional('baz')->equal_to('does_not_exist')->is_valid,
'not valid';
is_deeply $validation->output, {foo => 'bar'}, 'right result';
ok $validation->has_error, 'has error';
is_deeply $validation->error('baz'), [qw(equal_to does_not_exist)],
is_deeply $validation->error('baz'), [qw(equal_to 1 does_not_exist)],
'right error';
ok !$validation->optional('yada')->equal_to('foo')->is_valid, 'not valid';
is_deeply $validation->output, {foo => 'bar'}, 'right result';
ok $validation->has_error, 'has error';
is_deeply $validation->error('yada'), [qw(equal_to foo)], 'right error';
is_deeply $validation->error('yada'), [qw(equal_to 1 foo)], 'right error';

# In
$validation = $t->app->validation;
Expand All @@ -74,7 +74,7 @@ ok !$validation->has_error, 'no error';
ok !$validation->required('baz')->in(qw(yada whatever))->is_valid, 'not valid';
is_deeply $validation->output, {foo => [qw(bar whatever)]}, 'right result';
ok $validation->has_error, 'has error';
is_deeply $validation->error('baz'), [qw(in yada whatever)], 'right error';
is_deeply $validation->error('baz'), [qw(in 1 yada whatever)], 'right error';

# Like
$validation = $t->app->validation;
Expand All @@ -86,7 +86,7 @@ my $re = qr/ar$/;
ok !$validation->required('baz')->like($re)->is_valid, 'not valid';
is_deeply $validation->output, {foo => 'bar'}, 'right result';
ok $validation->has_error, 'has error';
is_deeply $validation->error('baz'), ['like', $re], 'right error';
is_deeply $validation->error('baz'), ['like', 1, $re], 'right error';

# Size
$validation = $t->app->validation;
Expand All @@ -97,13 +97,13 @@ ok !$validation->has_error, 'no error';
ok !$validation->required('baz')->size(1, 3)->is_valid, 'not valid';
is_deeply $validation->output, {foo => 'bar'}, 'right result';
ok $validation->has_error, 'has error';
is_deeply $validation->error('baz'), [qw(size 1 3)], 'right error';
is_deeply $validation->error('baz'), [qw(size 1 1 3)], 'right error';
ok !$validation->required('yada')->size(5, 10)->is_valid, 'not valid';
is $validation->topic, 'yada', 'right topic';
ok $validation->has_error('baz'), 'has error';
is_deeply $validation->output, {foo => 'bar'}, 'right result';
ok $validation->has_error, 'has error';
is_deeply $validation->error('yada'), [qw(size 5 10)], 'right error';
is_deeply $validation->error('yada'), [qw(size 1 5 10)], 'right error';

# Multiple empty values
$validation = $t->app->validation;
Expand Down Expand Up @@ -139,7 +139,7 @@ $t->get_ok('/' => form => {foo => '☃☃'})->status_is(200)

# Validation failed for required fields
$t->post_ok('/' => form => {foo => 'no'})->status_is(200)
->text_is('div:root' => 'in')
->text_is('div:root' => 'in 1')
->text_is('label.custom.field-with-error[for="foo"]' => '<Foo>')
->element_exists('input.custom.field-with-error[type="text"][value="no"]')
->element_exists_not('textarea.field-with-error')
Expand All @@ -149,7 +149,7 @@ $t->post_ok('/' => form => {foo => 'no'})->status_is(200)

# Failed validation for all fields
$t->get_ok('/?foo=too_long&bar=too_long_too&baz=way_too_long&yada=whatever')
->status_is(200)->text_is('div:root' => 'two')
->status_is(200)->text_is('div:root' => 'two ohoh')
->text_is('label.custom.field-with-error[for="foo"]' => '<Foo>')
->element_exists('input.custom.field-with-error[type="text"]')
->element_exists('textarea.field-with-error')
Expand All @@ -163,7 +163,10 @@ __DATA__
@@ index.html.ep
% if (validation->has_error('foo')) {
<div><%= validation->error('foo')->[0] %></div>
<div>
%= validation->error('foo')->[0]
%= validation->error('foo')->[1]
</div>
% }
%= form_for index => begin
%= label_for foo => '<Foo>', class => 'custom'
Expand Down

0 comments on commit d2cce28

Please sign in to comment.