line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormFu::Constraint::Repeatable::Any; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
588
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
166
|
|
4
|
|
|
|
|
|
|
our $VERSION = '2.05'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
18
|
use Moose; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
343
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
extends 'HTML::FormFu::Constraint'; |
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
16811
|
use Scalar::Util qw( reftype ); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
218
|
|
11
|
4
|
|
|
4
|
|
20
|
use Carp qw( croak ); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
1853
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
after BUILD => sub { |
14
|
|
|
|
|
|
|
my $self = shift; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
$self->only_on_reps( [1] ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
return; |
19
|
|
|
|
|
|
|
}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub process { |
22
|
34
|
|
|
34
|
0
|
33
|
my ( $self, $params ) = @_; |
23
|
|
|
|
|
|
|
|
24
|
34
|
100
|
|
|
|
67
|
return unless $self->_run_this_rep; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# check when condition |
27
|
16
|
50
|
|
|
|
72
|
return if !$self->_process_when($params); |
28
|
|
|
|
|
|
|
|
29
|
16
|
|
|
|
|
32
|
my $field = $self->field; |
30
|
16
|
|
|
|
|
67
|
my $repeatable = $field->get_parent( { type => 'Repeatable' } ); |
31
|
16
|
|
|
|
|
27
|
my $pass; |
32
|
|
|
|
|
|
|
|
33
|
16
|
|
100
|
|
|
402
|
my $original_name = $field->original_name || ''; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my @fields |
36
|
46
|
|
|
|
|
127
|
= grep { $_->get_parent( { type => 'Repeatable' } ) == $repeatable } |
37
|
52
|
|
100
|
|
|
1166
|
grep { ( $_->original_name || '' ) eq $original_name } |
38
|
16
|
|
|
|
|
21
|
@{ $repeatable->get_fields }; |
|
16
|
|
|
|
|
48
|
|
39
|
|
|
|
|
|
|
|
40
|
16
|
|
|
|
|
429
|
my $increment_field_names = $repeatable->increment_field_names; |
41
|
|
|
|
|
|
|
|
42
|
16
|
|
|
|
|
27
|
for my $f (@fields) { |
43
|
28
|
|
|
|
|
23
|
my $value; |
44
|
|
|
|
|
|
|
|
45
|
28
|
100
|
|
|
|
37
|
if ($increment_field_names) { |
46
|
22
|
|
|
|
|
51
|
$value = $self->get_nested_hash_value( $params, $f->nested_name ); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
else { |
49
|
6
|
|
|
|
|
10
|
$value = _find_this_field_value( $self, $f, $repeatable, $params ); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
28
|
|
|
|
|
32
|
my $ok = eval { $self->constrain_value($value) }; |
|
28
|
|
|
|
|
51
|
|
53
|
|
|
|
|
|
|
|
54
|
28
|
100
|
66
|
|
|
81
|
if ( $ok && !$@ ) { |
55
|
12
|
|
|
|
|
13
|
$pass = 1; |
56
|
12
|
|
|
|
|
15
|
last; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
16
|
|
|
|
|
67
|
return $self->mk_errors( { pass => $pass, } ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _find_this_field_value { |
64
|
6
|
|
|
6
|
|
6
|
my ( $self, $field, $repeatable, $params ) = @_; |
65
|
|
|
|
|
|
|
|
66
|
6
|
|
|
|
|
11
|
my $nested_name = $field->nested_name; |
67
|
|
|
|
|
|
|
|
68
|
6
|
|
|
|
|
21
|
my $value = $self->get_nested_hash_value( $params, $nested_name ); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my @fields_with_this_name |
71
|
6
|
|
|
|
|
8
|
= @{ $repeatable->get_fields( { nested_name => $nested_name } ) }; |
|
6
|
|
|
|
|
20
|
|
72
|
|
|
|
|
|
|
|
73
|
6
|
50
|
|
|
|
14
|
if ( @fields_with_this_name > 1 ) { |
74
|
6
|
|
|
|
|
3
|
my $index; |
75
|
|
|
|
|
|
|
|
76
|
6
|
|
|
|
|
13
|
for ( my $i = 0; $i <= $#fields_with_this_name; ++$i ) { |
77
|
8
|
100
|
|
|
|
16
|
if ( $fields_with_this_name[$i] eq $field ) { |
78
|
6
|
|
|
|
|
6
|
$index = $i; |
79
|
6
|
|
|
|
|
7
|
last; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
6
|
50
|
|
|
|
17
|
croak 'did not find ourself - how can this happen?' |
84
|
|
|
|
|
|
|
if !defined $index; |
85
|
|
|
|
|
|
|
|
86
|
6
|
50
|
|
|
|
13
|
if ( reftype($value) eq 'ARRAY' ) { |
|
|
0
|
|
|
|
|
|
87
|
6
|
|
|
|
|
9
|
$value = $value->[$index]; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
elsif ( $index == 0 ) { |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# keep $value |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
|
|
|
|
0
|
undef $value; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
6
|
|
|
|
|
9
|
return $value; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub constrain_value { |
102
|
28
|
|
|
28
|
0
|
25
|
my ( $self, $value ) = @_; |
103
|
|
|
|
|
|
|
|
104
|
28
|
|
66
|
|
|
101
|
return defined $value && length $value; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _localize_args { |
108
|
1
|
|
|
1
|
|
2
|
my ($self) = @_; |
109
|
|
|
|
|
|
|
|
110
|
1
|
|
33
|
|
|
3
|
return $self->parent->label || $self->parent->original_name || $self->parent->name; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 NAME |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
HTML::FormFu::Constraint::Repeatable::Any - Ensure at least 1 of a repeated field is filled-in |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 VERSION |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
version 2.05 |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 SYNOPSIS |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
elements: |
130
|
|
|
|
|
|
|
- type: Repeatable |
131
|
|
|
|
|
|
|
elements: |
132
|
|
|
|
|
|
|
- name: foo |
133
|
|
|
|
|
|
|
constraints: |
134
|
|
|
|
|
|
|
- type: Repeatable::Any |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 DESCRIPTION |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Ensure at least 1 of a repeated field is filled-in. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Any error will be attached to the first repetition of the field. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This constraint doesn't honour the C<not()> value. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 SEE ALSO |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Is a sub-class of, and inherits methods from L<HTML::FormFu::Constraint> |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
L<HTML::FormFu> |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 AUTHOR |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Carl Franks C<cfranks@cpan.org> |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 LICENSE |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify it under |
157
|
|
|
|
|
|
|
the same terms as Perl itself. |