I just discovered a little bug in the MinMaxFields constraint. The code that sets the default value for $max assumes that $others is an array ref:

    my $max = defined $self->maximum ? $self->maximum
            :                          1 + scalar @$others;

However, $others can sometimes be a single value, which is accounted for later in the code:

    push @names, ref $others ? @{$others} : $others;


Patch attached. Includes a new test that fails with the existing code and succeeds with the patch applied.

Ronald
diff -rN -x '*~' -u 
HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Constraint/MinMaxFields.pm 
HTML-FormFu-0.03005/lib/HTML/FormFu/Constraint/MinMaxFields.pm
--- HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Constraint/MinMaxFields.pm 
2008-09-03 10:26:04.000000000 -0400
+++ HTML-FormFu-0.03005/lib/HTML/FormFu/Constraint/MinMaxFields.pm      
2008-11-21 15:16:36.000000000 -0500
@@ -28,18 +28,18 @@
     my $others = $self->others;
     return if !defined $others;
 
+    # get field names to check
+    my @names = ( $self->nested_name );
+    push @names, ref $others ? @{$others} : $others;
+
     # get min/max values
     my $min = defined $self->minimum ? $self->minimum
             :                          1
             ;
 
     my $max = defined $self->maximum ? $self->maximum
-            :                          1 + scalar @$others;
+            :                          scalar @names;
     
-    # get field names to check
-    my @names = ( $self->nested_name );
-    push @names, ref $others ? @{$others} : $others;
-
     for my $name (@names) {
         my $value = $self->get_nested_hash_value( $params, $name );
 
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/t/constraints/minmaxfields.t 
HTML-FormFu-0.03005/t/constraints/minmaxfields.t
--- HTML-FormFu-0.03005.orig/t/constraints/minmaxfields.t       2008-05-29 
05:23:06.000000000 -0400
+++ HTML-FormFu-0.03005/t/constraints/minmaxfields.t    2008-11-21 
15:20:21.000000000 -0500
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 7;
+use Test::More tests => 8;
 
 use HTML::FormFu;
 
@@ -50,3 +50,20 @@
     ok( $form->valid('baz') );
     ok( $form->valid('boz') );
 }
+
+
+# Test setting default for max when others is a single element
+$form = HTML::FormFu->new;
+
+$form->element('Text')->name('foo')->constraint('MinMaxFields')
+    ->others('bar');
+$form->element('Text')->name('bar');
+
+{
+    $form->process( {
+            foo => 1,
+            bar => '',
+    } );
+
+    ok( !$form->has_errors );
+}
_______________________________________________
HTML-FormFu mailing list
HTML-FormFu@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu

Reply via email to