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