Skip to content

Commit

Permalink
test both arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 20, 2016
1 parent f105b2c commit 3471dd8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
7 changes: 3 additions & 4 deletions lib/Mojolicious/Validator/Validation.pm
Expand Up @@ -68,8 +68,7 @@ sub optional {

my $input = $self->input->{$name};
my @input = ref $input eq 'ARRAY' ? @$input : $input;
for my $filter (@filters) {
my $cb = $self->validator->filters->{$filter};
for my $cb (map { $self->validator->filters->{$_} } @filters) {
@input = map { $self->$cb($name, $_) } @input;
}
$self->output->{$name} = @input > 1 ? \@input : $input[0]
Expand Down Expand Up @@ -228,7 +227,7 @@ the current L</"topic">.
Change validation L</"topic"> and apply filters.
# Trim value
# Trim value and check size
$validation->optional('user', 'trim')->size(1, 15);
=head2 param
Expand All @@ -255,7 +254,7 @@ Return a list of all names for values that passed validation.
Change validation L</"topic">, apply filters, and make sure a value is present
and not an empty string.
# Trim value
# Trim value and check size
$validation->required('user', 'trim')->size(1, 15);
=head1 AUTOLOAD
Expand Down
9 changes: 5 additions & 4 deletions t/mojolicious/validation_lite_app.t
Expand Up @@ -9,7 +9,7 @@ use Mojolicious::Lite;
use Test::Mojo;

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

any '/' => sub {
my $c = shift;
Expand Down Expand Up @@ -174,11 +174,12 @@ ok $validation->required('nothing')->is_valid, 'valid';
is_deeply $validation->output, {nothing => ' '}, 'right result';

# Custom filter
$t->app->validator->add_filter(quote => sub {qq{"$_[2]"}});
$t->app->validator->add_filter(quote => sub {qq{$_[1]="$_[2]"}});
$validation = $t->app->validation->input({foo => [' bar', 'baz']});
ok $validation->required('foo', 'trim', 'quote')->like(qr/"/)->is_valid,
'valid';
is_deeply $validation->output, {foo => ['"bar"', '"baz"']}, 'right result';
is_deeply $validation->output, {foo => ['foo="bar"', 'foo="baz"']},
'right result';

# Multiple empty values
$validation = $t->app->validation;
Expand Down Expand Up @@ -325,7 +326,7 @@ $t->app->helper(
}
);
$t->get_ok('/?foo=too_long&bar=too_long_too&baz=way_too_long&yada=whatever')
->status_is(200)->text_is('div:root' => 'two ohoh')
->status_is(200)->text_is('div:root' => 'two e:foo')
->text_is('label.custom.my-field-with-error[for="foo"]' => '<Foo>')
->element_exists('input.custom.my-field-with-error[type="text"]')
->element_exists('textarea.my-field-with-error')
Expand Down

0 comments on commit 3471dd8

Please sign in to comment.