FormHandler/Fields.pm

filter: Statementstmt 86.7% 98 / 113 Branchbran 78.0% 39 / 50 Conditioncond 78.0% 32 / 41 MC/DCmcdc 56.5% 13 / 23 Subroutinesub 81.8% 18 / 22 Podpod 18.7% 3 / 16 totaltotal 76.6% 203 / 265 CC 49 SCAR 22.3
CC
49
Cov
83%
dump_validated
20.0
fields_fif
15.4
field
14.4
CRAP 9.3
« FormHandler/Field.pm ↑ Summary FormHandler/Widget/Theme/BootstrapFormMessages.pm »
1 package HTML::FormHandler::Fields;
2 # ABSTRACT: internal role for form and compound fields
3 146 $HTML::FormHandler::Fields::VERSION = '0.410000';
4 146 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 146 ▶use HTML::FormHandler::TraitFor::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 has 'fields' => (
9 traits => ['Array'],
10 isa => 'ArrayRef[HTML::FormHandler::Field]',
11 is => 'rw',
12 408 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 146 auto_deref => 1,
14 handles => {
15 all_fields => 'elements',
16 clear_fields => 'clear',
17 add_field => 'push',
18 push_field => 'push',
19 num_fields => 'count',
20 has_fields => 'count',
21 set_field_at => 'set',
22 _pop_field => 'pop',
23 }
24 );
25 # This is for updates applied via roles or compound field classes; allows doing
26 # both updates on the process call and updates from class applied roles
27 146 has 'update_subfields' => ( is => 'rw', isa => 'HashRef', builder => 'build_update_subfields',
28 traits => ['Hash'], handles => { clear_update_subfields => 'clear',
29 has_update_subfields => 'count' } );
30 382 ▶sub build_update_subfields {{}}
Subroutine build_update_subfields: called CC=1 SCAR=0.0
Pod undocumented
31  
32 # used to transfer tags to fields from form and compound fields
33 has 'widget_tags' => (
34 isa => 'HashRef',
35 traits => ['Hash'],
36 is => 'rw',
37 407 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
38 146 handles => {
39 has_widget_tags => 'count'
40 }
41 );
42  
43 # compatibility wrappers for result errors
44 sub error_fields {
45 14 ▶ my $self = shift;
Subroutine error_fields: called CC=1 SCAR=0.0
Pod undocumented
46 14 return map { $_->field_def } @{ $self->result->error_results };
47 }
48 20 ▶sub has_error_fields { shift->result->has_error_results }
Subroutine has_error_fields: called CC=1 SCAR=0.0
Pod undocumented
49  
50 sub add_error_field {
51 0 ▶ my ( $self, $field ) = @_;
Subroutine add_error_field: not called CC=1 SCAR=6.9
Pod undocumented
52 0 $self->result->add_error_result( $field->result );
53 }
54 6 ▶sub num_error_fields { shift->result->num_error_results }
Subroutine num_error_fields: called CC=1 SCAR=0.0
Pod undocumented
55  
56 146 has 'field_name_space' => (
57 isa => 'HFH::ArrayRefStr',
58 is => 'rw',
59 traits => ['Array'],
60 lazy => 1,
61 default => '',
62 coerce => 1,
63 handles => {
64 add_field_name_space => 'push',
65 },
66 );
67  
68 sub field_index {
69 1102 ▶ my ( $self, $name ) = @_;
Subroutine field_index: called CC=3 SCAR=11.0
Pod documented
70 1102 my $index = 0;
71 1102 for my $field ( $self->all_fields ) {
72 2059 ▶ return $index if $field->name eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $field->name eq $name 14 2045 2059
73 2045 $index++;
74 }
75 1088 return;
76 }
77  
78 sub subfield {
79 11 ▶ my ( $self, $name ) = @_;
Subroutine subfield: called CC=1 SCAR=0.0
Pod undocumented
80 11 return $self->field($name, undef, $self);
81 }
82  
83 sub field {
84 860 ▶ my ( $self, $name, $die, $f ) = @_;
Subroutine field: called CC=13 SCAR=26.6
Pod documented
85  
86 860 my $index;
87 # if this is a full_name for a compound field
88 # walk through the fields to get to it
89 860 ▶ return undef unless ( defined $name );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 860 860
90 860 ▶ if( $self->form && $self == $self->form &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form and $self == $self->form and exists $self->index->{$name}) 753 107 860
Condition83%5/6
$self->form and $self == $self->form and exists $self->index->{$name}
!ll&&!rl&&r
989753
$self->form and $self == $self->form
!ll&&!rl&&r
098762
MC/DC66%2/3
$self->form and $self == $self->form and exists $self->index->{$name}
$self->form $self == $self->form exists $self->index->{$name}
Truth table75%3/4
A and B and C
A= $self->formB= $self == $self->formC= exists $self->index->{$name}
ABCresult
0XX0
10X0
1100
1111
91 exists $self->index->{$name} ) {
92 753 return $self->index->{$name};
93 }
94 107 ▶ if ( $name =~ /\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\./) { } 12 95 107
95 12 my @names = split /\./, $name;
96 12 ▶ $f ||= $self->form || $self;
Condition50%3/6
$f ||= $self->form || $self
l!l&&r!l&&!r
480
$self->form || $self
l!l&&r!l&&!r
800
MC/DC0%0/3
$f ||= $self->form || $self
$f $self->form $self
Truth table0%0/4
A ||= B || C
A= $fB= $self->formC= $self
ABCresult
1XX1
01X1
0011
0000
97 12 foreach my $fname (@names) {
98 30 $f = $f->field($fname);
99 30 ▶ return unless $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 30 30
100 }
101 12 return $f;
102 }
103 else # not a compound name
104 {
105 95 for my $field ( $self->all_fields ) {
106 135 ▶ return $field if ( $field->name eq $name );
Branch: 1 branch
BranchTrueFalseTotal
if $field->name eq $name 92 43 135
107 }
108 }
109 3 ▶ return unless $die;
Branch: 1 branch
BranchTrueFalseTotal
unless $die 3 0 3
110 0 die "Field '$name' not found in '$self'";
111 }
112  
113 sub sorted_fields {
114 940 ▶ my $self = shift;
Subroutine sorted_fields: called CC=2 SCAR=6.9
Pod documented
115  
116 2904 my @fields = sort { $a->order <=> $b->order }
117 940 grep { $_->is_active } $self->all_fields;
118 940 ▶ return wantarray ? @fields : \@fields;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 938 2 940
119 }
120  
121 # the routine for looping through and processing each field
122 sub _fields_validate {
123 268 ▶ my $self = shift;
Subroutine _fields_validate: called CC=8 SCAR=20.8
124  
125 268 ▶ return unless $self->has_fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_fields 0 268 268
126 # validate all fields
127 268 my %value_hash;
128 268 foreach my $field ( $self->all_fields ) {
129 838 ▶ next if ( $field->is_inactive || $field->disabled || !$field->has_result );
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_inactive or $field->disabled or not $field->has_result 4 824 828
Condition83%5/6
$field->is_inactive or $field->disabled or not $field->has_result
l!l&&r!l&&!r
140824
$field->is_inactive or $field->disabled
l!l&&r!l&&!r
104824
MC/DC66%2/3
$field->is_inactive or $field->disabled or not $field->has_result
$field->is_inactive $field->disabled not $field->has_result
Truth table75%3/4
A or B or not(C)
A= $field->is_inactiveB= $field->disabledC= not $field->has_result
ABCresult
1XX1
01X1
0011
0000
130 # Validate each field and "inflate" input -> value.
131 824 $field->validate_field; # this calls the field's 'validate' routine
132 824 ▶ $value_hash{ $field->accessor } = $field->value
Branch: 1 branch
BranchTrueFalseTotal
if $field->has_value and not $field->noupdate 733 91 824
Condition100%3/3
$field->has_value and not $field->noupdate
!ll&&!rl&&r
7417733
MC/DC100%2/2
$field->has_value and not $field->noupdate
$field->has_value not $field->noupdate
Truth table100%3/3
A and not(B)
A= $field->has_valueB= not $field->noupdate
ABresult
0X0
100
111
133 if ( $field->has_value && !$field->noupdate );
134 }
135 268 $self->_set_value( \%value_hash );
136 }
137  
138 sub fields_set_value {
139 177 ▶ my $self = shift;
Subroutine fields_set_value: called CC=6 SCAR=17.9
Pod undocumented
140 177 my %value_hash;
141 177 foreach my $field ( $self->all_fields ) {
142 577 ▶ next if ( $field->is_inactive || !$field->has_result );
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_inactive or not $field->has_result 10 567 577
Condition66%2/3
$field->is_inactive or not $field->has_result
l!l&&r!l&&!r
100567
MC/DC50%1/2
$field->is_inactive or not $field->has_result
$field->is_inactive not $field->has_result
Truth table66%2/3
A or not(B)
A= $field->is_inactiveB= not $field->has_result
ABresult
1X1
011
000
143 567 ▶ $value_hash{ $field->accessor } = $field->value
Branch: 1 branch
BranchTrueFalseTotal
if $field->has_value and not $field->noupdate 487 80 567
Condition100%3/3
$field->has_value and not $field->noupdate
!ll&&!rl&&r
6317487
MC/DC100%2/2
$field->has_value and not $field->noupdate
$field->has_value not $field->noupdate
Truth table100%3/3
A and not(B)
A= $field->has_valueB= not $field->noupdate
ABresult
0X0
100
111
144 if ( $field->has_value && !$field->noupdate );
145 }
146 177 $self->_set_value( \%value_hash );
147 }
148  
149 sub fields_fif {
150 146 ▶ my ( $self, $result, $prefix ) = @_;
Subroutine fields_fif: called CC=15 SCAR=27.3
Pod undocumented
151  
152 146 ▶ $result ||= $self->result;
Condition66%2/3
$result ||= $self->result
l!l&&r!l&&!r
70760
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table66%2/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
153 146 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 146 146
154 146 ▶ $prefix ||= '';
Condition100%2/2
$prefix ||= ""
l!l
6977
MC/DC100%1/1
$prefix ||= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
155 146 ▶ if ( $self->isa('HTML::FormHandler') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("HTML::FormHandler")) 77 69 146
156 77 ▶ $prefix = $self->name . "." if $self->html_prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $self->html_prefix 0 77 77
157 }
158 146 my %params;
159 146 foreach my $fld_result ( $result->results ) {
160 413 my $field = $fld_result->field_def;
161 413 ▶ next if ( $field->is_inactive || $field->password );
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_inactive or $field->password 0 413 413
Condition33%1/3
$field->is_inactive or $field->password
l!l&&r!l&&!r
00413
MC/DC0%0/2
$field->is_inactive or $field->password
$field->is_inactive $field->password
Truth table33%1/3
A or B
A= $field->is_inactiveB= $field->password
ABresult
1X1
011
000
162 413 my $fif = $fld_result->fif;
163 413 ▶ next if ( !defined $fif || (ref $fif eq 'ARRAY' && ! scalar @{$fif} ) );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fif or ref $fif eq "ARRAY" and not scalar @{$fif;} 10 403 413
Condition100%6/6
not defined $fif or ref $fif eq "ARRAY" and not scalar @{$fif;}
l!l&&r!l&&!r
75401
ref $fif eq "ARRAY" and not scalar @{$fif;}
!ll&&!rl&&r
376273
MC/DC100%3/3
not defined $fif or ref $fif eq "ARRAY" and not scalar @{$fif;}
not defined $fif ref $fif eq "ARRAY" not scalar @{$fif;}
Truth table100%4/4
not(A) or B and not(C)
A= not defined $fifB= ref $fif eq "ARRAY"C= not scalar @{$fif;}
ABCresult
1XX1
0111
00X0
0100
164 403 ▶ if ( $fld_result->has_results ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fld_result->has_results) { } 69 334 403
165 69 my $next_params = $fld_result->fields_fif( $prefix . $field->name . '.' );
166 69 ▶ next unless $next_params;
Branch: 1 branch
BranchTrueFalseTotal
unless $next_params 1 68 69
167 68 %params = ( %params, %{$next_params} );
168 }
169 else {
170 334 $params{ $prefix . $field->name } = $fif;
171 }
172 }
173 146 ▶ return if !%params;
Branch: 1 branch
BranchTrueFalseTotal
unless %params 2 144 146
174 144 return \%params;
175 }
176  
177 sub clear_data {
178 212 ▶ my $self = shift;
Subroutine clear_data: called CC=2 SCAR=6.9
Pod undocumented
179 212 $self->clear_result;
180 212 $self->clear_active;
181 212 $_->clear_data for $self->all_fields;
182 }
183  
184 sub propagate_error {
185 138 ▶ my ( $self, $result ) = @_;
Subroutine propagate_error: called CC=3 SCAR=11.0
Pod undocumented
186  
187 # References to fields with errors are propagated up the tree.
188 # All fields with errors should end up being in the form's
189 # error_results. Once.
190 138 my ($found) = grep { $_ == $result } $self->result->all_error_results;
191 138 ▶ unless ( $found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 125 13 138
192 125 $self->result->add_error_result($result);
193 125 ▶ if ( $self->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parent) 20 105 125
194 20 $self->parent->propagate_error( $result );
195 }
196 }
197 }
198  
199 0 ▶sub dump_fields { shift->dump(@_) }
Subroutine dump_fields: not called CC=1 SCAR=6.9
Pod undocumented
200  
201 sub dump {
202 0 ▶ my $self = shift;
Subroutine dump: not called CC=2 SCAR=17.9
Pod undocumented
203  
204 0 warn "HFH: ------- fields for ", $self->name, "-------\n";
205 0 for my $field ( $self->sorted_fields ) {
206 0 $field->dump;
207 }
208 0 warn "HFH: ------- end fields -------\n";
209 }
210  
211 sub dump_validated {
212 0 ▶ my $self = shift;
Subroutine dump_validated: not called CC=4 SCAR=30.0
Pod undocumented
213 0 warn "HFH: fields validated:\n";
214 0 foreach my $field ( $self->sorted_fields ) {
215 0 ▶ $field->dump_validated if $field->can('dump_validated');
Branch: 1 branch
BranchTrueFalseTotal
if $field->can("dump_validated") 0 0 0
216 0 ▶ my $message = $field->has_errors ? join( ' | ', $field->all_errors) : 'validated';
Branch: 1 branch
BranchTrueFalseTotal
$field->has_errors ? : 0 0 0
217 0 warn "HFH: ", $field->name, ": $message\n";
218 }
219 }
220  
221 146 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
222 146 1;
223  
224 __END__
225  
226 =pod
227  
228 =encoding UTF-8
229  
230 =head1 NAME
231  
232 HTML::FormHandler::Fields - internal role for form and compound fields
233  
234 =head1 VERSION
235  
236 version 0.410000
237  
238 =head1 SYNOPSIS
239  
240 A role to implement field attributes, accessors, etc. To be applied
241 to L<HTML::FormHandler> and L<HTML::FormHandler::Field::Compound>.
242  
243 =head2 fields
244  
245 The field definitions as built from the field_list and the 'has_field'
246 declarations. This provides clear_fields, add_field, remove_last_field,
247 num_fields, has_fields, and set_field_at methods.
248  
249 =head2 field( $full_name )
250  
251 Return the field object with the full_name passed. Will return undef
252 if the field is not found, or will die if passed a second parameter.
253  
254 =head2 field_index
255  
256 Convenience function for use with 'set_field_at'. Pass in 'name' of field
257 (not full_name)
258  
259 =head2 sorted_fields
260  
261 Calls fields and returns them in sorted order by their "order"
262 value. Non-sorted fields are retrieved with 'fields'.
263  
264 =head2 clear methods
265  
266 clear_data
267 clear_fields
268 clear_error_fields
269  
270 =head2 Dump information
271  
272 dump - turn verbose flag on to get this output
273 dump_validated - shorter version
274  
275 =head1 AUTHOR
276  
277 FormHandler Contributors - see HTML::FormHandler
278  
279 =head1 COPYRIGHT AND LICENSE
280  
281 This software is copyright (c) 2017 by Gerda Shank.
282  
283 This is free software; you can redistribute it and/or modify it under
284 the same terms as the Perl 5 programming language system itself.
285  
286 =cut
« FormHandler/Field.pm ↑ Summary FormHandler/Widget/Theme/BootstrapFormMessages.pm »