Skip to content

Commit

Permalink
stick with the original validation behavior for now
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 7, 2015
1 parent 523ed7a commit 97f6f44
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
4 changes: 1 addition & 3 deletions Changes
@@ -1,7 +1,5 @@

6.15 2015-08-07
- Fixed bug in Mojolicious::Validator::Validation where empty optional values
were not being handled correctly.
6.15 2015-08-08
- Fixed warnings in Mojo::DOM.

6.14 2015-07-12
Expand Down
26 changes: 11 additions & 15 deletions lib/Mojolicious/Validator/Validation.pm
Expand Up @@ -63,31 +63,27 @@ sub has_error { $_[1] ? exists $_[0]{error}{$_[1]} : !!keys %{$_[0]{error}} }

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

sub optional { shift->_copy(shift, 0) }
sub optional {
my ($self, $name) = @_;

my $input = $self->input->{$name};
my @input = ref $input eq 'ARRAY' ? @$input : $input;
$self->output->{$name} = $input
unless grep { !defined($_) || !length($_) } @input;

return $self->topic($name);
}

sub param { shift->every_param(shift)->[-1] }

sub passed { [sort keys %{shift->output}] }

sub required {
my ($self, $name) = @_;
return $self if $self->_copy($name, 1)->is_valid;
return $self if $self->optional($name)->is_valid;
return $self->error($name => ['required']);
}

sub _copy {
my ($self, $name, $all) = @_;

my $input = $self->input->{$name};
my @old = ref $input eq 'ARRAY' ? @$input : $input;
my @new = grep { defined && length } @old;

if ($all && @new && @old == @new) { $self->output->{$name} = $input }
elsif (!$all && @new) { $self->output->{$name} = @new > 1 ? \@new : $new[0] }

return $self->topic($name);
}

1;

=encoding utf8
Expand Down
8 changes: 2 additions & 6 deletions t/mojolicious/validation_lite_app.t
Expand Up @@ -159,12 +159,8 @@ $validation = $t->app->validation;
ok !$validation->has_data, 'no data';
$validation->input({foo => ['', 'bar', ''], bar => ['', 'baz', '']});
ok $validation->has_data, 'has data';
ok $validation->optional('bar')->is_valid, 'valid';
ok $validation->in('baz')->is_valid, 'still valid';
is_deeply $validation->output, {bar => 'baz'}, 'right result';
ok !$validation->has_error, 'no error';
ok !$validation->required('foo')->is_valid, 'not valid';
is_deeply $validation->output, {bar => 'baz'}, 'right result';
is_deeply $validation->output, {}, 'right result';
ok $validation->has_error, 'has error';
is_deeply $validation->error('foo'), ['required'], 'right error';

Expand Down Expand Up @@ -227,7 +223,7 @@ $t->get_ok('/' => form => {foo => '☃☃'})->status_is(200)
->element_exists('input[type="password"]');

# Validation failed for required fields
$t->post_ok('/' => form => {foo => ['no']})->status_is(200)
$t->post_ok('/' => form => {foo => 'no'})->status_is(200)
->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"]')
Expand Down

0 comments on commit 97f6f44

Please sign in to comment.