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