Subject: | New curry-style constraint for multivalued params |
It recently became necessary to prevent array refs from getting past DFV and into Params::Validate later on (causing failure). Here's a patch with two constraints--FV_num_vals and FV_num_vals_between--that can validate the number of values for a given param.
Subject: | num_vals.diff |
82,83d81
< FV_num_values
< FV_num_values_between
345,401d342
< =head2 FV_num_values
<
< use Data::FormValidator::Constraints qw ( FV_num_values );
<
< constraint_methods => {
< attachments => FV_num_values(4),
< }
<
< Checks the number of values in the array named by this param.
< Note that this is useful for making sure that only one value was passed for a
< given param (by supplying a size argument of 1).
< A constraint name of C<num_values> will be set.
<
< =cut
<
< sub FV_num_values {
< my $size = shift || croak 'size argument is required';
< return sub {
< my $dfv = shift;
< $dfv->name_this('num_values');
< my $param = $dfv->get_current_constraint_field();
< my $value = $dfv->get_filtered_data()->{$param};
<
< return scalar @$value == $size if ref $value eq 'ARRAY';
< return 1 if $size == 1;
< return 0;
< }
< }
<
< =head2 FV_num_values_between
<
< use Data::FormValidator::Constraints qw ( FV_num_values_between );
<
< constraint_methods => {
< attachments => FV_num_values_between(1,4),
< }
<
< Checks that the number of values in the array named by this param is between
< the supplied bounds (exclusively).
< A constraint name of C<num_values_between> will be set.
<
< =cut
<
< sub FV_num_values_between {
< my ($min, $max) = @_;
< croak 'min and max arguments are required' unless $min && $max;
< return sub {
< my $dfv = shift;
< $dfv->name_this('num_values_between');
< my $param = $dfv->get_current_constraint_field();
< my $value = $dfv->get_filtered_data()->{$param};
<
< return scalar @$value > $min && scalar @$value < $max if ref $value eq 'ARRAY';
< return 1 if $min == 0 && $max >= 2; # scalar, size could be 1
< return 0; # scalar, size can't be 1
< }
< }