Permalink
Browse files

fix bug in required_when when field value is 0

  • Loading branch information...
1 parent 0dde88b commit 089f6100d472cf48f260a2763ab9b52a14919594 @gshank committed Sep 11, 2012
Showing with 23 additions and 4 deletions.
  1. +4 −3 lib/HTML/FormHandler/Validate.pm
  2. +19 −1 t/validation/reqwhen.t
@@ -263,20 +263,21 @@ sub match_when {
my $from_form = ( $key =~ /^\+/ );
$key =~ s/^\+//;
my $field = $from_form ? $self->form->field($key) : $self->parent->subfield( $key );
+ my $field_fif = defined $field->fif ? $field->fif : '';
unless ( $field ) {
warn "field '$key' not found processing when";
next;
}
if ( ref $check_against eq 'CODE' ) {
$matched++
- if $check_against->($field->fif, $self);
+ if $check_against->($field_fif, $self);
}
elsif ( ref $check_against eq 'ARRAY' ) {
foreach my $value ( @$check_against ) {
- $matched++ if ( $value eq $field->fif );
+ $matched++ if ( $value eq $field_fif );
}
}
- elsif ( $check_against eq ( $field->fif || '' ) ) {
+ elsif ( $check_against eq $field_fif ) {
$matched++;
}
else {
View
@@ -32,12 +32,30 @@ ok( ! $form->validated, 'did not validate when fee is 2' );
}
-$form = MyApp::Form::Test->new;
+$form = MyApp::Form::Test2->new;
$params = { fee => '', fie => '' };
$form->process( $params );
ok( $form->validated );
$params = { fee => 2, fie => '' };
$form->process( $params );
ok( ! $form->validated, 'did not validate when fee is 2' );
+# handle required when value of '0'
+{
+ package MyApp::Form::Test3;
+ use HTML::FormHandler::Moose;
+ extends 'HTML::FormHandler';
+
+ has_field 'fee';
+ has_field 'fie' => ( required_when => { 'fee' => 0 } );
+
+}
+$form = MyApp::Form::Test3->new;
+$params = { fee => 0, fie => '' };
+$form->process( $params );
+ok( !$form->validated );
+$params = { fee => 1, fie => '' };
+$form->process( $params );
+ok( $form->validated );
+
done_testing;

0 comments on commit 089f610

Please sign in to comment.