[html-formfu] [patch] bug in Constraint::MinMaxFields
Ronald J Kimball
rkimball+formfu at pangeamedia.com
Fri Nov 21 20:28:51 GMT 2008
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 =3D 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
-------------- next part --------------
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Constraint/Min=
MaxFields.pm HTML-FormFu-0.03005/lib/HTML/FormFu/Constraint/MinMaxFields.pm
--- HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Constraint/MinMaxFields.pm 200=
8-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 =3D $self->others;
return if !defined $others;
=
+ # get field names to check
+ my @names =3D ( $self->nested_name );
+ push @names, ref $others ? @{$others} : $others;
+
# get min/max values
my $min =3D defined $self->minimum ? $self->minimum
: 1
;
=
my $max =3D defined $self->maximum ? $self->maximum
- : 1 + scalar @$others;
+ : scalar @names;
=
- # get field names to check
- my @names =3D ( $self->nested_name );
- push @names, ref $others ? @{$others} : $others;
-
for my $name (@names) {
my $value =3D $self->get_nested_hash_value( $params, $name );
=
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/t/constraints/minmaxfields.t H=
TML-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.00=
0000000 -0500
@@ -1,7 +1,7 @@
use strict;
use warnings;
=
-use Test::More tests =3D> 7;
+use Test::More tests =3D> 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 =3D HTML::FormFu->new;
+
+$form->element('Text')->name('foo')->constraint('MinMaxFields')
+ ->others('bar');
+$form->element('Text')->name('bar');
+
+{
+ $form->process( {
+ foo =3D> 1,
+ bar =3D> '',
+ } );
+
+ ok( !$form->has_errors );
+}
More information about the HTML-FormFu
mailing list