File Coverage

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


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.40067';
4 141     141   67838 use Moose::Role;
  141         437  
  141         825  
5 141     141   487112 use HTML::FormHandler::TraitFor::Types;
  141         1195  
  141         149065  
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 369     369 0 107476 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 397 my $self = shift;
46 14         19 return map { $_->field_def } @{ $self->result->error_results };
  29         718  
  14         400  
47             }
48 19     19 0 477 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 172 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 1078     1078 1 2338 my ( $self, $name ) = @_;
70 1078         1540 my $index = 0;
71 1078         37489 for my $field ( $self->all_fields ) {
72 2039 100       46716 return $index if $field->name eq $name;
73 2025         2197 $index++;
74             }
75 1064         2452 return;
76             }
77              
78             sub subfield {
79 11     11 0 39 my ( $self, $name ) = @_;
80 11         34 return $self->field($name, undef, $self);
81             }
82              
83             sub field {
84 856     856 1 85202 my ( $self, $name, $die, $f ) = @_;
85              
86 856         968 my $index;
87             # if this is a full_name for a compound field
88             # walk through the fields to get to it
89 856 50       1969 return undef unless ( defined $name );
90 856 100 66     4728 if( $self->form && $self == $self->form &&
      66        
91             exists $self->index->{$name} ) {
92 749         15788 return $self->index->{$name};
93             }
94 107 100       268 if ( $name =~ /\./ ) {
95 12         49 my @names = split /\./, $name;
96 12   33     57 $f ||= $self->form || $self;
      66        
97 12         25 foreach my $fname (@names) {
98 30         118 $f = $f->field($fname);
99 30 50       73 return unless $f;
100             }
101 12         161 return $f;
102             }
103             else # not a compound name
104             {
105 95         2890 for my $field ( $self->all_fields ) {
106 135 100       3278 return $field if ( $field->name eq $name );
107             }
108             }
109 3 50       19 return unless $die;
110 0         0 die "Field '$name' not found in '$self'";
111             }
112              
113             sub sorted_fields {
114 914     914 1 1199 my $self = shift;
115              
116 2876         63762 my @fields = sort { $a->order <=> $b->order }
117 914         30014 grep { $_->is_active } $self->all_fields;
  2731         8035  
118 914 100       5700 return wantarray ? @fields : \@fields;
119             }
120              
121             # the routine for looping through and processing each field
122             sub _fields_validate {
123 256     256   378 my $self = shift;
124              
125 256 50       8230 return unless $self->has_fields;
126             # validate all fields
127 256         384 my %value_hash;
128 256         7788 foreach my $field ( $self->all_fields ) {
129 816 100 100     2153 next if ( $field->is_inactive || $field->disabled || !$field->has_result );
      66        
130             # Validate each field and "inflate" input -> value.
131 802         3407 $field->validate_field; # this calls the field's 'validate' routine
132 802 100 100     2388 $value_hash{ $field->accessor } = $field->value
133             if ( $field->has_value && !$field->noupdate );
134             }
135 256         2052 $self->_set_value( \%value_hash );
136             }
137              
138             sub fields_set_value {
139 165     165 0 274 my $self = shift;
140 165         244 my %value_hash;
141 165         5267 foreach my $field ( $self->all_fields ) {
142 555 100 66     1462 next if ( $field->is_inactive || !$field->has_result );
143 545 100 100     1722 $value_hash{ $field->accessor } = $field->value
144             if ( $field->has_value && !$field->noupdate );
145             }
146 165         831 $self->_set_value( \%value_hash );
147             }
148              
149             sub fields_fif {
150 146     146 0 224 my ( $self, $result, $prefix ) = @_;
151              
152 146   66     2298 $result ||= $self->result;
153 146 50       307 return unless $result;
154 146   100     426 $prefix ||= '';
155 146 100       680 if ( $self->isa('HTML::FormHandler') ) {
156 77 50       1991 $prefix = $self->name . "." if $self->html_prefix;
157             }
158 146         176 my %params;
159 146         4859 foreach my $fld_result ( $result->results ) {
160 413         10509 my $field = $fld_result->field_def;
161 413 50 33     967 next if ( $field->is_inactive || $field->password );
162 413         1082 my $fif = $fld_result->fif;
163 413 100 100     1534 next if ( !defined $fif || (ref $fif eq 'ARRAY' && ! scalar @{$fif} ) );
  30   66     104  
164 403 100       12529 if ( $fld_result->has_results ) {
165 69         1619 my $next_params = $fld_result->fields_fif( $prefix . $field->name . '.' );
166 69 100       150 next unless $next_params;
167 68         127 %params = ( %params, %{$next_params} );
  68         454  
168             }
169             else {
170 334         7553 $params{ $prefix . $field->name } = $fif;
171             }
172             }
173 146 100       766 return if !%params;
174 144         554 return \%params;
175             }
176              
177             sub clear_data {
178 211     211 0 1373 my $self = shift;
179 211         5657 $self->clear_result;
180 211         6298 $self->clear_active;
181 211         6221 $_->clear_data for $self->all_fields;
182             }
183              
184             sub propagate_error {
185 123     123 0 190 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 123         2798 my ($found) = grep { $_ == $result } $self->result->all_error_results;
  79         176  
191 123 100       333 unless ( $found ) {
192 111         2409 $self->result->add_error_result($result);
193 111 100       2700 if ( $self->parent ) {
194 20         447 $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 141     141   865 use namespace::autoclean;
  141         407  
  141         1095  
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.40067
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) 2016 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