File Coverage

blib/lib/HTML/FormHandler/Fields.pm
Criterion Covered Total %
statement 90 105 85.7
branch 39 50 78.0
condition 32 41 78.0
subroutine 16 20 80.0
pod 3 16 18.7
total 180 232 77.5


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Fields;
2             # ABSTRACT: internal role for form and compound fields
3             $HTML::FormHandler::Fields::VERSION = '0.40068';
4 143     143   89245 use Moose::Role;
  143         409  
  143         1476  
5 143     143   786236 use HTML::FormHandler::TraitFor::Types;
  143         413  
  143         163614  
6              
7              
8             has 'fields' => (
9             traits => ['Array'],
10             isa => 'ArrayRef[HTML::FormHandler::Field]',
11             is => 'rw',
12             default => sub { [] },
13             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             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 371     371 0 173527 sub build_update_subfields {{}}
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             default => sub {{}},
38             handles => {
39             has_widget_tags => 'count'
40             }
41             );
42              
43             # compatibility wrappers for result errors
44             sub error_fields {
45 14     14 0 10237 my $self = shift;
46 14         39 return map { $_->field_def } @{ $self->result->error_results };
  29         1046  
  14         670  
47             }
48 20     20 0 642 sub has_error_fields { shift->result->has_error_results }
49              
50             sub add_error_field {
51 0     0 0 0 my ( $self, $field ) = @_;
52 0         0 $self->result->add_error_result( $field->result );
53             }
54 6     6 0 197 sub num_error_fields { shift->result->num_error_results }
55              
56             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 1084     1084 1 3629 my ( $self, $name ) = @_;
70 1084         3020 my $index = 0;
71 1084         42018 for my $field ( $self->all_fields ) {
72 2046 100       51931 return $index if $field->name eq $name;
73 2032         4212 $index++;
74             }
75 1070         3626 return;
76             }
77              
78             sub subfield {
79 11     11 0 62 my ( $self, $name ) = @_;
80 11         62 return $self->field($name, undef, $self);
81             }
82              
83             sub field {
84 858     858 1 115675 my ( $self, $name, $die, $f ) = @_;
85              
86 858         1837 my $index;
87             # if this is a full_name for a compound field
88             # walk through the fields to get to it
89 858 50       2846 return undef unless ( defined $name );
90 858 100 66     5867 if( $self->form && $self == $self->form &&
      100        
91             exists $self->index->{$name} ) {
92 751         18295 return $self->index->{$name};
93             }
94 107 100       387 if ( $name =~ /\./ ) {
95 12         68 my @names = split /\./, $name;
96 12   33     72 $f ||= $self->form || $self;
      66        
97 12         33 foreach my $fname (@names) {
98 30         181 $f = $f->field($fname);
99 30 50       118 return unless $f;
100             }
101 12         221 return $f;
102             }
103             else # not a compound name
104             {
105 95         3196 for my $field ( $self->all_fields ) {
106 135 100       3470 return $field if ( $field->name eq $name );
107             }
108             }
109 3 50       20 return unless $die;
110 0         0 die "Field '$name' not found in '$self'";
111             }
112              
113             sub sorted_fields {
114 919     919 1 2298 my $self = shift;
115              
116 2887         71432 my @fields = sort { $a->order <=> $b->order }
117 919         32610 grep { $_->is_active } $self->all_fields;
  2745         12484  
118 919 100       5360 return wantarray ? @fields : \@fields;
119             }
120              
121             # the routine for looping through and processing each field
122             sub _fields_validate {
123 258     258   645 my $self = shift;
124              
125 258 50       9185 return unless $self->has_fields;
126             # validate all fields
127 258         647 my %value_hash;
128 258         8787 foreach my $field ( $self->all_fields ) {
129 822 100 100     3354 next if ( $field->is_inactive || $field->disabled || !$field->has_result );
      66        
130             # Validate each field and "inflate" input -> value.
131 808         4789 $field->validate_field; # this calls the field's 'validate' routine
132 808 100 100     3478 $value_hash{ $field->accessor } = $field->value
133             if ( $field->has_value && !$field->noupdate );
134             }
135 258         2674 $self->_set_value( \%value_hash );
136             }
137              
138             sub fields_set_value {
139 167     167 0 481 my $self = shift;
140 167         693 my %value_hash;
141 167         6244 foreach my $field ( $self->all_fields ) {
142 561 100 66     2037 next if ( $field->is_inactive || !$field->has_result );
143 551 100 100     2399 $value_hash{ $field->accessor } = $field->value
144             if ( $field->has_value && !$field->noupdate );
145             }
146 167         1096 $self->_set_value( \%value_hash );
147             }
148              
149             sub fields_fif {
150 146     146 0 437 my ( $self, $result, $prefix ) = @_;
151              
152 146   66     2708 $result ||= $self->result;
153 146 50       464 return unless $result;
154 146   100     712 $prefix ||= '';
155 146 100       1142 if ( $self->isa('HTML::FormHandler') ) {
156 77 50       2583 $prefix = $self->name . "." if $self->html_prefix;
157             }
158 146         322 my %params;
159 146         5241 foreach my $fld_result ( $result->results ) {
160 413         11535 my $field = $fld_result->field_def;
161 413 50 33     1442 next if ( $field->is_inactive || $field->password );
162 413         1626 my $fif = $fld_result->fif;
163 413 100 100     2027 next if ( !defined $fif || (ref $fif eq 'ARRAY' && ! scalar @{$fif} ) );
  30   100     234  
164 403 100       13547 if ( $fld_result->has_results ) {
165 69         1770 my $next_params = $fld_result->fields_fif( $prefix . $field->name . '.' );
166 69 100       239 next unless $next_params;
167 68         215 %params = ( %params, %{$next_params} );
  68         645  
168             }
169             else {
170 334         8363 $params{ $prefix . $field->name } = $fif;
171             }
172             }
173 146 100       911 return if !%params;
174 144         815 return \%params;
175             }
176              
177             sub clear_data {
178 212     212 0 1968 my $self = shift;
179 212         6371 $self->clear_result;
180 212         7173 $self->clear_active;
181 212         6891 $_->clear_data for $self->all_fields;
182             }
183              
184             sub propagate_error {
185 125     125 0 402 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 125         3365 my ($found) = grep { $_ == $result } $self->result->all_error_results;
  79         292  
191 125 100       452 unless ( $found ) {
192 113         2985 $self->result->add_error_result($result);
193 113 100       3733 if ( $self->parent ) {
194 20         527 $self->parent->propagate_error( $result );
195             }
196             }
197             }
198              
199 0     0 0   sub dump_fields { shift->dump(@_) }
200              
201             sub dump {
202 0     0 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             sub dump_validated {
212 0     0 0   my $self = shift;
213 0           warn "HFH: fields validated:\n";
214 0           foreach my $field ( $self->sorted_fields ) {
215 0 0         $field->dump_validated if $field->can('dump_validated');
216 0 0         my $message = $field->has_errors ? join( ' | ', $field->all_errors) : 'validated';
217 0           warn "HFH: ", $field->name, ": $message\n";
218             }
219             }
220              
221 143     143   1468 use namespace::autoclean;
  143         526  
  143         1763  
222             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.40068
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