Debian Codegraph @

lib/HTML/FormHandler/Fields.pm

raw
1 package HTML::FormHandler::Fields;
2 # ABSTRACT: internal role for form and compound fields
3 $HTML::FormHandler::Fields::VERSION = '0.410002';
4 use Moose::Role;
5 use HTML::FormHandler::TraitFor::Types;
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 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 my $self = shift;
46 return map { $_->field_def } @{ $self->result->error_results };
47 }
48 sub has_error_fields { shift->result->has_error_results }
49
50 sub add_error_field {
51 my ( $self, $field ) = @_;
52 $self->result->add_error_result( $field->result );
53 }
54 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 my ( $self, $name ) = @_;
70 my $index = 0;
71 for my $field ( $self->all_fields ) {
72 return $index if $field->name eq $name;
73 $index++;
74 }
75 return;
76 }
77
78 sub subfield {
79 my ( $self, $name ) = @_;
80 return $self->field($name, undef, $self);
81 }
82
83 sub field {
84 my ( $self, $name, $die, $f ) = @_;
85
86 my $index;
87 # if this is a full_name for a compound field
88 # walk through the fields to get to it
89 return undef unless ( defined $name );
90 if( $self->form && $self == $self->form &&
91 exists $self->index->{$name} ) {
92 return $self->index->{$name};
93 }
94 if ( $name =~ /\./ ) {
95 my @names = split /\./, $name;
96 $f ||= $self->form || $self;
97 foreach my $fname (@names) {
98 $f = $f->field($fname);
99 return unless $f;
100 }
101 return $f;
102 }
103 else # not a compound name
104 {
105 for my $field ( $self->all_fields ) {
106 return $field if ( $field->name eq $name );
107 }
108 }
109 return unless $die;
110 die "Field '$name' not found in '$self'";
111 }
112
113 sub sorted_fields {
114 my $self = shift;
115
116 my @fields = sort { $a->order <=> $b->order }
117 grep { $_->is_active } $self->all_fields;
118 return wantarray ? @fields : \@fields;
119 }
120
121 # the routine for looping through and processing each field
122 sub _fields_validate {
123 my $self = shift;
124
125 return unless $self->has_fields;
126 # validate all fields
127 my %value_hash;
128 foreach my $field ( $self->all_fields ) {
129 next if ( $field->is_inactive || $field->disabled || !$field->has_result );
130 # Validate each field and "inflate" input -> value.
131 $field->validate_field; # this calls the field's 'validate' routine
132 $value_hash{ $field->accessor } = $field->value
133 if ( $field->has_value && !$field->noupdate );
134 }
135 $self->_set_value( \%value_hash );
136 }
137
138 sub fields_set_value {
139 my $self = shift;
140 my %value_hash;
141 foreach my $field ( $self->all_fields ) {
142 next if ( $field->is_inactive || !$field->has_result );
143 $value_hash{ $field->accessor } = $field->value
144 if ( $field->has_value && !$field->noupdate );
145 }
146 $self->_set_value( \%value_hash );
147 }
148
149 sub fields_fif {
150 my ( $self, $result, $prefix ) = @_;
151
152 $result ||= $self->result;
153 return unless $result;
154 $prefix ||= '';
155 if ( $self->isa('HTML::FormHandler') ) {
156 $prefix = $self->name . "." if $self->html_prefix;
157 }
158 my %params;
159 foreach my $fld_result ( $result->results ) {
160 my $field = $fld_result->field_def;
161 next if ( $field->is_inactive || $field->password );
162 my $fif = $fld_result->fif;
163 next if ( !defined $fif || (ref $fif eq 'ARRAY' && ! scalar @{$fif} ) );
164 if ( $fld_result->has_results ) {
165 my $next_params = $fld_result->fields_fif( $prefix . $field->name . '.' );
166 next unless $next_params;
167 %params = ( %params, %{$next_params} );
168 }
169 else {
170 $params{ $prefix . $field->name } = $fif;
171 }
172 }
173 return if !%params;
174 return \%params;
175 }
176
177 sub clear_data {
178 my $self = shift;
179 $self->clear_result;
180 $self->clear_active;
181 $_->clear_data for $self->all_fields;
182 }
183
184 sub propagate_error {
185 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 my ($found) = grep { $_ == $result } $self->result->all_error_results;
191 unless ( $found ) {
192 $self->result->add_error_result($result);
193 if ( $self->parent ) {
194 $self->parent->propagate_error( $result );
195 }
196 }
197 }
198
199 sub dump_fields { shift->dump(@_) }
200
201 sub dump {
202 my $self = shift;
203
204 warn "HFH: ------- fields for ", $self->name, "-------\n";
205 for my $field ( $self->sorted_fields ) {
206 $field->dump;
207 }
208 warn "HFH: ------- end fields -------\n";
209 }
210
211 sub dump_validated {
212 my $self = shift;
213 warn "HFH: fields validated:\n";
214 foreach my $field ( $self->sorted_fields ) {
215 $field->dump_validated if $field->can('dump_validated');
216 my $message = $field->has_errors ? join( ' | ', $field->all_errors) : 'validated';
217 warn "HFH: ", $field->name, ": $message\n";
218 }
219 }
220
221 use namespace::autoclean;
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.410002
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