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 147 $HTML::FormHandler::Fields::VERSION = '0.410002';
4 147 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 147 ▶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 409 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 147 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 147 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 383 ▶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 408 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
38 147 handles => {
39 has_widget_tags => 'count'
40 }
41 );
42  
43 # compatibility wrappers for result errors
44 14 ▶sub error_fields {
Subroutine error_fields: called CC=1 SCAR=0.0
Pod undocumented
45 14 my $self = shift;
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 0 ▶sub add_error_field {
Subroutine add_error_field: not called CC=1 SCAR=6.9
Pod undocumented
51 0 my ( $self, $field ) = @_;
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 147 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 1106 ▶sub field_index {
Subroutine field_index: called CC=3 SCAR=11.0
Pod documented
69 1106 my ( $self, $name ) = @_;
70 1106 my $index = 0;
71 1106 for my $field ( $self->all_fields ) {
72 2065 ▶ return $index if $field->name eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $field->name eq $name 14 2051 2065
73 2051 $index++;
74 }
75 1092 return;
76 }
77  
78 11 ▶sub subfield {
Subroutine subfield: called CC=1 SCAR=0.0
Pod undocumented
79 11 my ( $self, $name ) = @_;
80 11 return $self->field($name, undef, $self);
81 }
82  
83 864 ▶sub field {
Subroutine field: called CC=13 SCAR=26.6
Pod documented
84 864 my ( $self, $name, $die, $f ) = @_;
85  
86 864 my $index;
87 # if this is a full_name for a compound field
88 # walk through the fields to get to it
89 864 ▶ return undef unless ( defined $name );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 864 864
90 864 ▶ if( $self->form && $self == $self->form &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form and $self == $self->form and exists $self->index->{$name}) 757 107 864
Condition83%5/6
$self->form and $self == $self->form and exists $self->index->{$name}
!ll&&!rl&&r
989757
$self->form and $self == $self->form
!ll&&!rl&&r
098766
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 757 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 942 ▶sub sorted_fields {
Subroutine sorted_fields: called CC=2 SCAR=6.9
Pod documented
114 942 my $self = shift;
115  
116 2912 my @fields = sort { $a->order <=> $b->order }
117 942 grep { $_->is_active } $self->all_fields;
118 942 ▶ return wantarray ? @fields : \@fields;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 940 2 942
119 }
120  
121 # the routine for looping through and processing each field
122 268 ▶sub _fields_validate {
Subroutine _fields_validate: called CC=8 SCAR=20.8
123 268 my $self = shift;
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 14 824 838
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 177 ▶sub fields_set_value {
Subroutine fields_set_value: called CC=6 SCAR=17.9
Pod undocumented
139 177 my $self = shift;
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 146 ▶sub fields_fif {
Subroutine fields_fif: called CC=15 SCAR=27.3
Pod undocumented
150 146 my ( $self, $result, $prefix ) = @_;
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
73403
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 212 ▶sub clear_data {
Subroutine clear_data: called CC=2 SCAR=6.9
Pod undocumented
178 212 my $self = shift;
179 212 $self->clear_result;
180 212 $self->clear_active;
181 212 $_->clear_data for $self->all_fields;
182 }
183  
184 138 ▶sub propagate_error {
Subroutine propagate_error: called CC=3 SCAR=11.0
Pod undocumented
185 138 my ( $self, $result ) = @_;
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 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod undocumented
202 0 my $self = shift;
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 0 ▶sub dump_validated {
Subroutine dump_validated: not called CC=4 SCAR=30.0
Pod undocumented
212 0 my $self = shift;
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 147 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
222 147 1;
223  
224 __END__
« FormHandler/Field.pm ↑ Summary FormHandler/Widget/Theme/BootstrapFormMessages.pm »