1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
package HTML::FormFu::Constraint::MinMaxFields;
$HTML::FormFu::Constraint::MinMaxFields::VERSION = '2.01';
use Moose;
use MooseX::Attribute::FormFuChained;
use MooseX::Aliases;
extends 'HTML::FormFu::Constraint';
with 'HTML::FormFu::Role::Constraint::Others';
use HTML::FormFu::Util qw( DEBUG_CONSTRAINTS debug );
has minimum => (
is => 'rw',
alias => 'min',
traits => ['FormFuChained'],
);
has maximum => (
is => 'rw',
alias => 'max',
traits => ['FormFuChained'],
);
after BUILD => sub {
my $self = shift;
$self->attach_errors_to_base(1);
return;
};
sub process {
my ( $self, $params ) = @_;
my $count = 0;
# check when condition
return if !$self->_process_when($params);
# others are needed
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
: scalar @names;
for my $name (@names) {
my $value = $self->get_nested_hash_value( $params, $name );
DEBUG_CONSTRAINTS && debug( OTHER_NAME => $name );
DEBUG_CONSTRAINTS && debug( VALUE => $value );
if ( ref $value eq 'ARRAY' ) {
my @errors = eval { $self->constrain_values( $value, $params ) };
if ( !@errors && !$@ ) {
$count++;
}
}
else {
my $ok = eval { $self->constrain_value($value) };
if ( $ok && !$@ ) {
$count++;
}
}
}
my $pass = ( $count < $min || $count > $max ) ? 0 : 1;
return $self->mk_errors( {
pass => $pass,
failed => $pass ? [] : \@names,
names => \@names,
} );
}
# return true if value is defined
sub constrain_value {
my ( $self, $value ) = @_;
return 0 if !defined $value || $value eq '';
return 1;
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=head1 NAME
HTML::FormFu::Constraint::MinMaxFields - Min/Max Multi-field Constraint
=head1 SYNOPSIS
type: MinMaxFields
name: foo
others: [bar, baz]
min: 1
max: 1
=head1 DESCRIPTION
Ensure that at least a minimum and only a maximum number of fields are
present.
This constraint doesn't honour the C<not()> value.
=head1 METHODS
=head2 minimum
=head2 min
The minimum number of named fields which must be filled in.
L</min> is an alias for L</minimum>.
=head2 maximum
=head2 max
The maximum number of named fields which must be filled in.
L</max> is an alias for L</maximum>.
The default for max is the number of all affected fields, in other words one
more than the number of elements given to others.
=head2 attach_errors_to_base
Default Value: 1
=head2 attach_errors_to_others
Default Value: 0
=head1 SEE ALSO
Is a sub-class of, and inherits methods from
L<HTML::FormFu::Role::Constraint::Others>, L<HTML::FormFu::Constraint>
L<HTML::FormFu>
=head1 AUTHOR
Mario Minati C<mario.minati@googlemail.com>
=head1 LICENSE
This library is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|