FormHandler.pm

filter: Statementstmt 88.0% 398 / 452 Branchbran 68.3% 108 / 158 Conditioncond 67.6% 46 / 68 MC/DCmcdc 47.7% 21 / 44 Subroutinesub 83.8% 78 / 93 Podpod 14.5% 8 / 55 totaltotal 75.7% 659 / 870 CC 121 SCAR 19.4
CC
121
Cov
81%
setup_form
16.5
BUILD
16.2
build_active
13.9
CRAP 6.9
« FormHandler/Field/Date.pm ↑ Summary FormHandler/Render/Simple.pm »
1 package HTML::FormHandler;
2 # ABSTRACT: HTML forms using Moose
3 146 $HTML::FormHandler::VERSION = '0.410001';
4 146 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 146 extends 'HTML::FormHandler::Base'; # to make some methods overridable by roles
6 146 with 'HTML::FormHandler::Model', 'HTML::FormHandler::Fields',
7 'HTML::FormHandler::BuildFields',
8 'HTML::FormHandler::TraitFor::I18N';
9 146 with 'HTML::FormHandler::InitResult';
10 146 with 'HTML::FormHandler::Widget::ApplyRole';
11 146 with 'HTML::FormHandler::Traits';
12 146 with 'HTML::FormHandler::Blocks';
13  
14 146 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 146 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 146 ▶use HTML::FormHandler::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 146 ▶use HTML::FormHandler::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 146 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 146 ▶use MooseX::Types::LoadableClass qw/ LoadableClass /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 146 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 146 ▶use HTML::FormHandler::Merge ('merge');
Subroutine BEGIN: called CC=1 SCAR=0.0
22 146 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 146 ▶use Data::Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 146 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 # for consistency in api with field nodes
29 17717 ▶sub form { shift }
Subroutine form: called CC=1 SCAR=0.0
Pod documented
30 0 ▶sub is_form { 1 }
Subroutine is_form: not called CC=1 SCAR=6.9
Pod undocumented
31 0 ▶sub has_form { 1 }
Subroutine has_form: not called CC=1 SCAR=6.9
Pod undocumented
32  
33 # Moose attributes
34 has 'name' => (
35 isa => 'Str',
36 is => 'rw',
37 197 ▶ default => sub { return 'form' . int( rand 1000 ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
38 146 );
39 4199 ▶sub full_name { '' }
Subroutine full_name: called CC=1 SCAR=0.0
Pod undocumented
40 10 ▶sub full_accessor { '' }
Subroutine full_accessor: called CC=1 SCAR=0.0
Pod undocumented
41 146 has 'parent' => ( is => 'rw' );
42 146 has 'result' => (
43 isa => 'HTML::FormHandler::Result',
44 is => 'ro',
45 writer => '_set_result',
46 clearer => 'clear_result',
47 lazy => 1,
48 builder => 'build_result',
49 predicate => 'has_result',
50 handles => [
51 'input', '_set_input', '_clear_input', 'has_input',
52 'value', '_set_value', '_clear_value', 'has_value',
53 'add_result', 'results', 'validated', 'ran_validation',
54 'is_valid',
55 'form_errors', 'all_form_errors', 'push_form_errors', 'clear_form_errors',
56 'has_form_errors', 'num_form_errors',
57 ],
58 );
59  
60 sub build_result {
61 567 ▶ my $self = shift;
Subroutine build_result: called CC=2 SCAR=7.0
Pod undocumented
62 567 my $result_class = 'HTML::FormHandler::Result';
63 567 ▶ if ( $self->widget_form ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->widget_form) 567 0 567
64 567 my $role = $self->get_widget_role( $self->widget_form, 'Form' );
65 567 $result_class = $result_class->with_traits( $role );
66 }
67 567 my $result = $result_class->new( name => $self->name, form => $self );
68 567 return $result;
69 }
70  
71 has 'index' => (
72 is => 'ro', isa => 'HashRef[HTML::FormHandler::Field]', traits => ['Hash'],
73 259 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
74 146 handles => {
75 add_to_index => 'set',
76 field_from_index => 'get',
77 field_in_index => 'exists',
78 }
79 );
80 has '_repeatable_fields' => ( is => 'rw', isa => 'ArrayRef',
81 259 ▶ traits => ['Array'], default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
82 146 handles => {
83 add_repeatable_field => 'push',
84 has_repeatable_fields => 'count',
85 all_repeatable_fields => 'elements',
86 },
87 );
88  
89 has 'field_traits' => ( is => 'ro', traits => ['Array'], isa => 'ArrayRef',
90 256 ▶ default => sub {[]}, handles => { 'has_field_traits' => 'count' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
91 has 'widget_name_space' => (
92 is => 'ro',
93 isa => 'HFH::ArrayRefStr',
94 traits => ['Array'],
95 253 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
96 146 coerce => 1,
97 handles => {
98 add_widget_name_space => 'push',
99 },
100 );
101 # it only really makes sense to set these before widget_form is applied in BUILD
102 146 has 'widget_form' => ( is => 'ro', isa => 'Str', default => 'Simple', writer => 'set_widget_form' );
103 146 has 'widget_wrapper' => ( is => 'ro', isa => 'Str', default => 'Simple', writer => 'set_widget_wrapper' );
104 146 has 'do_form_wrapper' => ( is => 'rw', builder => 'build_do_form_wrapper' );
105 248 ▶sub build_do_form_wrapper { 0 }
Subroutine build_do_form_wrapper: called CC=1 SCAR=0.0
Pod undocumented
106 146 has 'no_widgets' => ( is => 'ro', isa => 'Bool' );
107 146 has 'no_preload' => ( is => 'ro', isa => 'Bool' );
108 146 has 'no_update' => ( is => 'rw', isa => 'Bool', clearer => 'clear_no_update' );
109 has 'active' => (
110 is => 'rw',
111 traits => ['Array'],
112 isa => 'ArrayRef[Str]',
113 259 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
114 146 handles => {
115 add_active => 'push',
116 has_active => 'count',
117 clear_active => 'clear',
118 }
119 );
120 has 'inactive' => (
121 is => 'rw',
122 traits => ['Array'],
123 isa => 'ArrayRef[Str]',
124 258 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
125 146 handles => {
126 add_inactive => 'push',
127 has_inactive => 'count',
128 clear_inactive => 'clear',
129 }
130 );
131  
132  
133 # object with which to initialize
134 146 has 'init_object' => ( is => 'rw', clearer => 'clear_init_object' );
135 has 'update_field_list' => ( is => 'rw',
136 isa => 'HashRef',
137 259 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
138 146 traits => ['Hash'],
139 handles => {
140 clear_update_field_list => 'clear',
141 has_update_field_list => 'count',
142 set_update_field_list => 'set',
143 },
144 );
145 259 ▶has 'defaults' => ( is => 'rw', isa => 'HashRef', default => sub {{}}, traits => ['Hash'],
Subroutine __ANON__: called CC=1 SCAR=0.0
146 handles => { has_defaults => 'count', clear_defaults => 'clear' },
147 );
148 146 has 'use_defaults_over_obj' => ( is => 'rw', isa => 'Bool', clearer => 'clear_use_defaults_over_obj' );
149 146 has 'use_init_obj_over_item' => ( is => 'rw', isa => 'Bool', clearer => 'clear_use_init_obj_over_item' );
150 146 has 'use_init_obj_when_no_accessor_in_item' => ( is => 'rw', isa => 'Bool' );
151 146 has 'use_fields_for_input_without_param' => ( is => 'rw', isa => 'Bool' );
152 # flags
153 146 has [ 'verbose', 'processed', 'did_init_obj' ] => ( isa => 'Bool', is => 'rw' );
154 146 has 'user_data' => ( isa => 'HashRef', is => 'rw' );
155 146 has 'ctx' => ( is => 'rw', weak_ref => 1, clearer => 'clear_ctx' );
156 146 has 'html_prefix' => ( isa => 'Bool', is => 'ro' );
157 146 has 'active_column' => ( isa => 'Str', is => 'ro' );
158 146 has 'http_method' => ( isa => 'Str', is => 'ro', default => 'post' );
159 146 has 'enctype' => ( is => 'rw', isa => 'Str' );
160 146 has 'error_message' => ( is => 'rw', predicate => 'has_error_message', clearer => 'clear_error_message' );
161 146 has 'success_message' => ( is => 'rw', predicate => 'has_success_message', clearer => 'clear_success_message' );
162 146 has 'info_message' => ( is => 'rw', predicate => 'has_info_message', clearer => 'clear_info_message' );
163 # deprecated
164 146 has 'style' => ( isa => 'Str', is => 'rw' );
165  
166 146 has 'is_html5' => ( isa => 'Bool', is => 'ro', default => 0 );
167 # deprecated. use form_element_attr instead
168 has 'html_attr' => ( is => 'rw', traits => ['Hash'],
169 259 ▶ default => sub { {} }, handles => { has_html_attr => 'count',
Subroutine __ANON__: called CC=1 SCAR=0.0
170 set_html_attr => 'set', delete_html_attr => 'delete' },
171 trigger => \&_html_attr_set,
172 );
173 sub _html_attr_set {
174 0 ▶ my ( $self, $value ) = @_;
Subroutine _html_attr_set: not called CC=2 SCAR=17.9
175 0 my $class = delete $value->{class};
176 0 $self->form_element_attr($value);
177 0 ▶ $self->add_form_element_class if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 0 0 0
178 }
179  
180 {
181 # create the attributes and methods for
182 # form_element_attr, build_form_element_attr, form_element_class,
183 # form_wrapper_attr, build_form_wrapper_atrr, form_wrapper_class
184 146 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
185 146 foreach my $attr ('form_wrapper', 'form_element' ) {
186 292 my $add_meth = "add_${attr}_class";
187 292 has "${attr}_attr" => ( is => 'rw', traits => ['Hash'],
188 builder => "build_${attr}_attr",
189 handles => {
190 "has_${attr}_attr" => 'count',
191 "get_${attr}_attr" => 'get',
192 "set_${attr}_attr" => 'set',
193 "delete_${attr}_attr" => 'delete',
194 "exists_${attr}_attr" => 'exists',
195 },
196 );
197 # create builders for _attr
198 292 my $attr_builder = __PACKAGE__ . "::build_${attr}_attr";
199 178 ▶ *$attr_builder = subname $attr_builder, sub {{}};
Subroutine build_form_wrapper_attr: called CC=1 SCAR=0.0
Subroutine build_form_element_attr: called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
200 # create the 'class' slots
201 292 has "${attr}_class" => ( is => 'rw', isa => 'HFH::ArrayRefStr',
202 traits => ['Array'],
203 coerce => 1,
204 builder => "build_${attr}_class",
205 handles => {
206 "has_${attr}_class" => 'count',
207 "_add_${attr}_class" => 'push',
208 },
209 );
210 # create builders for classes
211 292 my $class_builder = __PACKAGE__ . "::build_${attr}_class";
212 191 ▶ *$class_builder = subname $class_builder, sub {[]};
Subroutine build_form_wrapper_class: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Pod undocumented
213 # create wrapper for add_to_ to accept arrayref
214 292 my $add_to_class = __PACKAGE__ . "::add_${attr}_class";
215 292 my $_add_meth = __PACKAGE__ . "::_add_${attr}_class";
216 # create add method that takes an arrayref
217 0 ▶ *$add_to_class = subname $add_to_class, sub { shift->$_add_meth((ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_)); }
Subroutine add_form_wrapper_class: not called CC=2 SCAR=17.9
Subroutine __ANON__: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
ref $_[0] eq 'ARRAY' ? : 0 0 0
218 292 }
219 }
220  
221 88 ▶sub attributes { shift->form_element_attributes(@_) }
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
222 sub form_element_attributes {
223 88 ▶ my ( $self, $result ) = @_;
Subroutine form_element_attributes: called CC=8 SCAR=21.0
Pod undocumented
224 88 ▶ $result ||= $self->result;
Condition66%2/3
$result ||= $self->result
l!l&&r!l&&!r
8170
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table66%2/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
225 88 my $attr = {};
226 88 $attr->{id} = $self->name;
227 88 ▶ $attr->{action} = $self->action if $self->action;
Branch: 1 branch
BranchTrueFalseTotal
if $self->action 1 87 88
228 88 ▶ $attr->{method} = $self->http_method if $self->http_method;
Branch: 1 branch
BranchTrueFalseTotal
if $self->http_method 88 0 88
229 88 ▶ $attr->{enctype} = $self->enctype if $self->enctype;
Branch: 1 branch
BranchTrueFalseTotal
if $self->enctype 0 88 88
230 88 ▶ $attr->{style} = $self->style if $self->style;
Branch: 1 branch
BranchTrueFalseTotal
if $self->style 8 80 88
231 88 $attr = {%$attr, %{$self->form_element_attr}};
232 88 my $class = [@{$self->form_element_class}];
233 88 ▶ $attr->{class} = $class if @$class;
Branch: 1 branch
BranchTrueFalseTotal
if @$class 14 74 88
234 88 my $mod_attr = $self->html_attributes($self, 'form_element', $attr);
235 88 ▶ return ref $mod_attr eq 'HASH' ? $mod_attr : $attr;
Branch: 1 branch
BranchTrueFalseTotal
ref $mod_attr eq 'HASH' ? : 88 0 88
236 }
237 sub form_wrapper_attributes {
238 15 ▶ my ( $self, $result ) = @_;
Subroutine form_wrapper_attributes: called CC=4 SCAR=14.1
Pod undocumented
239 15 ▶ $result ||= $self->result;
Condition33%1/3
$result ||= $self->result
l!l&&r!l&&!r
1500
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table33%1/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
240 15 my $attr = {%{$self->form_wrapper_attr}};
241 15 my $class = [@{$self->form_wrapper_class}];
242 15 ▶ $attr->{class} = $class if @$class;
Branch: 1 branch
BranchTrueFalseTotal
if @$class 12 3 15
243 15 my $mod_attr = $self->html_attributes($self, 'form_wrapper', $attr);
244 15 ▶ return ref $mod_attr eq 'HASH' ? $mod_attr : $attr;
Branch: 1 branch
BranchTrueFalseTotal
ref $mod_attr eq 'HASH' ? : 15 0 15
245 }
246  
247 sub html_attributes {
248 1417 ▶ my ( $self, $obj, $type, $attrs, $result ) = @_;
Subroutine html_attributes: called CC=2 SCAR=7.6
Pod undocumented
249 # deprecated 'field_html_attributes'; name changed, remove eventually
250 1417 ▶ if( $self->can('field_html_attributes') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("field_html_attributes")) 0 1417 1417
251 0 $attrs = $self->field_html_attributes( $obj, $type, $attrs, $result );
252 }
253 1417 return $attrs;
254 }
255  
256 sub has_flag {
257 2283 ▶ my ( $self, $flag_name ) = @_;
Subroutine has_flag: called CC=2 SCAR=6.9
Pod undocumented
258 2283 ▶ return unless $self->can($flag_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($flag_name) 1310 973 2283
259 973 return $self->$flag_name;
260 }
261  
262 146 has 'form_tags' => (
263 traits => ['Hash'],
264 isa => 'HashRef',
265 is => 'ro',
266 builder => 'build_form_tags',
267 handles => {
268 _get_tag => 'get',
269 set_tag => 'set',
270 tag_exists => 'exists',
271 has_tag => 'exists',
272 },
273 );
274 246 ▶sub build_form_tags {{}}
Subroutine build_form_tags: called CC=1 SCAR=0.0
Pod undocumented
275 sub get_tag {
276 682 ▶ my ( $self, $name ) = @_;
Subroutine get_tag: called CC=6 SCAR=24.2
Pod undocumented
277 682 ▶ return '' unless $self->tag_exists($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->tag_exists($name) 636 46 682
278 46 my $tag = $self->_get_tag($name);
279 46 ▶ return $self->$tag if ref $tag eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag eq "CODE" 0 46 46
280 46 ▶ return $tag unless $tag =~ /^%/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /^%/ 46 0 46
281 0 ( my $block_name = $tag ) =~ s/^%//;
282 0 ▶ return $self->form->block($block_name)->render
Branch: 1 branch
BranchTrueFalseTotal
if $self->form and $self->form->block_exists($block_name) 0 0 0
Condition0%0/3
$self->form and $self->form->block_exists($block_name)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->form and $self->form->block_exists($block_name)
$self->form $self->form->block_exists($block_name)
Truth table0%0/3
A and B
A= $self->formB= $self->form->block_exists($block_name)
ABresult
0X0
100
111
283 if ( $self->form && $self->form->block_exists($block_name) );
284 0 return '';
285 }
286 has 'for_js' => (
287 isa => 'HashRef',
288 traits => ['Hash'],
289 is => 'rw',
290 259 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
291 146 handles => {
292 set_for_js => 'set',
293 has_for_js => 'count',
294 clear_for_js => 'clear',
295 }
296 );
297  
298 146 has 'action' => ( is => 'rw' );
299 146 has 'posted' => ( is => 'rw', isa => 'Bool', clearer => 'clear_posted', predicate => 'has_posted' );
300 has 'params' => (
301 traits => ['Hash'],
302 isa => 'HashRef',
303 is => 'rw',
304 259 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
305 367 ▶ trigger => sub { shift->_munge_params(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
306 146 handles => {
307 set_param => 'set',
308 get_param => 'get',
309 clear_params => 'clear',
310 has_params => 'count',
311 },
312 );
313 0 ▶sub submitted { shift->has_params }
Subroutine submitted: not called CC=1 SCAR=6.9
Pod undocumented
314 146 has 'dependency' => ( isa => 'ArrayRef', is => 'rw' );
315 has '_required' => (
316 traits => ['Array'],
317 isa => 'ArrayRef[HTML::FormHandler::Field]',
318 is => 'rw',
319 259 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
320 146 handles => {
321 clear_required => 'clear',
322 add_required => 'push',
323 }
324 );
325  
326 # these messages could apply to either fields or form
327 146 has 'messages' => ( is => 'rw',
328 isa => 'HashRef',
329 traits => ['Hash'],
330 builder => 'build_messages',
331 handles => {
332 '_get_form_message' => 'get',
333 '_has_form_message' => 'exists',
334 'set_message' => 'set',
335 },
336 );
337 258 ▶sub build_messages { {} }
Subroutine build_messages: called CC=1 SCAR=0.0
Pod undocumented
338  
339 146 my $class_messages = {};
340 sub get_class_messages {
341 0 ▶ return $class_messages;
Subroutine get_class_messages: not called CC=1 SCAR=6.9
Pod undocumented
342 }
343  
344 sub get_message {
345 0 ▶ my ( $self, $msg ) = @_;
Subroutine get_message: not called CC=2 SCAR=17.9
Pod undocumented
346 0 ▶ return $self->_get_form_message($msg) if $self->_has_form_message($msg);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_form_message($msg) 0 0 0
347 0 return $self->get_class_messages->{$msg};
348 }
349 sub all_messages {
350 0 ▶ my $self = shift;
Subroutine all_messages: not called CC=1 SCAR=6.9
Pod undocumented
351 0 return { %{$self->get_class_messages}, %{$self->messages} };
352 }
353  
354 146 has 'params_class' => (
355 is => 'ro',
356 isa => LoadableClass,
357 coerce => 1,
358 default => 'HTML::FormHandler::Params',
359 );
360  
361 146 has 'params_args' => ( is => 'ro', isa => 'ArrayRef' );
362  
363 sub BUILDARGS {
364 259 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=4 SCAR=17.9
Pod documented
365  
366 259 ▶ if ( scalar @_ == 1 && ref( $_[0]) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] ne "HASH") 0 259 259
Condition66%2/3
scalar @_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
25720
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] ne "HASH"
scalar @_ == 1 ref $_[0] ne "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] ne "HASH"
ABresult
0X0
100
111
367 0 my $arg = $_[0];
368 0 ▶ return blessed($arg) ? { item => $arg } : { item_id => $arg };
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($arg) ? : 0 0 0
369 }
370 259 return $class->SUPER::BUILDARGS(@_);
371 }
372  
373 sub BUILD {
374 259 ▶ my $self = shift;
Subroutine BUILD: called CC=14 SCAR=27.8
Pod undocumented
375  
376 259 $self->before_build; # hook to allow customizing forms
377 # HTML::FormHandler::Widget::Form::Simple is applied in Base
378 259 ▶ $self->apply_widget_role( $self, $self->widget_form, 'Form' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_widgets or $self->widget_form eq "Simple" 3 256 259
Condition66%2/3
$self->no_widgets or $self->widget_form eq "Simple"
l!l&&r!l&&!r
02563
MC/DC50%1/2
$self->no_widgets or $self->widget_form eq "Simple"
$self->no_widgets $self->widget_form eq "Simple"
Truth table66%2/3
A or B
A= $self->no_widgetsB= $self->widget_form eq "Simple"
ABresult
1X1
011
000
379 unless ( $self->no_widgets || $self->widget_form eq 'Simple' );
380 259 $self->_build_fields; # create the form fields (BuildFields.pm)
381 256 ▶ $self->build_active if $self->has_active || $self->has_inactive || $self->has_flag('is_wizard');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_active or $self->has_inactive or $self->has_flag("is_wizard") 3 253 256
Condition83%5/6
$self->has_active or $self->has_inactive or $self->has_flag("is_wizard")
l!l&&r!l&&!r
12253
$self->has_active or $self->has_inactive
l!l&&r!l&&!r
01255
MC/DC66%2/3
$self->has_active or $self->has_inactive or $self->has_flag("is_wizard")
$self->has_active $self->has_inactive $self->has_flag("is_wizard")
Truth table75%3/4
A or B or C
A= $self->has_activeB= $self->has_inactiveC= $self->has_flag("is_wizard")
ABCresult
1XX1
01X1
0011
0000
382 256 $self->after_build; # hook for customizing
383 256 ▶ return if defined $self->item_id && !$self->item;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->item_id and not $self->item 0 256 256
Condition33%1/3
defined $self->item_id and not $self->item
!ll&&!rl&&r
25600
MC/DC0%0/2
defined $self->item_id and not $self->item
defined $self->item_id not $self->item
Truth table33%1/3
A and not(B)
A= defined $self->item_idB= not $self->item
ABresult
0X0
100
111
384 # Load values from object (if any)
385 # Would rather not load results at all here, but skipping it breaks
386 # existing apps that perform certain actions between 'new' and 'process'.
387 # Added fudge flag no_preload to enable skipping.
388 # A well-behaved program that always does ->process shouldn't need this preloading.
389 256 ▶ unless( $self->no_preload ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_preload) 255 1 256
390 255 ▶ if ( my $init_object = $self->use_init_obj_over_item ?
Branch: 2 branches
BranchTrueFalseTotal
if (my $init_object = $self->use_init_obj_over_item ? $self->init_object || $self->item : $self->item || $self->init_object) { } 10 245 255
$self->use_init_obj_over_item ? : 0 255 255
Condition50%3/6
$self->init_object or $self->item
l!l&&r!l&&!r
000
$self->item or $self->init_object
l!l&&r!l&&!r
19245
MC/DC0%0/2
$self->init_object or $self->item
$self->init_object $self->item
MC/DC100%2/2
$self->item or $self->init_object
$self->item $self->init_object
Truth table0%0/3
A or B
A= $self->init_objectB= $self->item
ABresult
1X1
011
000
Truth table100%3/3
A or B
A= $self->itemB= $self->init_object
ABresult
1X1
011
000
391 ($self->init_object || $self->item) : ( $self->item || $self->init_object ) ) {
392 10 $self->_result_from_object( $self->result, $init_object );
393 }
394 else {
395 245 $self->_result_from_fields( $self->result );
396 }
397 }
398 256 ▶ $self->dump_fields if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 256 256
399 256 return;
400 }
401 ▶sub before_build {}
Subroutine before_build: called CC=1 SCAR=0.0
Pod undocumented
402 ▶sub after_build {}
Subroutine after_build: called CC=1 SCAR=0.0
Pod undocumented
403  
404 sub process {
405 284 ▶ my $self = shift;
Subroutine process: called CC=9 SCAR=22.0
Pod documented
406  
407 284 ▶ warn "HFH: process ", $self->name, "\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 284 284
408 284 ▶ $self->clear if $self->processed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->processed 103 181 284
409 284 $self->setup_form(@_);
410 284 ▶ $self->validate_form if $self->posted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->posted 169 115 284
411 284 ▶ $self->update_model if ( $self->validated && !$self->no_update );
Branch: 1 branch
BranchTrueFalseTotal
if $self->validated and not $self->no_update 97 187 284
Condition100%3/3
$self->validated and not $self->no_update
!ll&&!rl&&r
186197
MC/DC100%2/2
$self->validated and not $self->no_update
$self->validated not $self->no_update
Truth table100%3/3
A and not(B)
A= $self->validatedB= not $self->no_update
ABresult
0X0
100
111
412 284 ▶ $self->after_update_model if ( $self->validated && !$self->no_update );
Branch: 1 branch
BranchTrueFalseTotal
if $self->validated and not $self->no_update 97 187 284
Condition100%3/3
$self->validated and not $self->no_update
!ll&&!rl&&r
186197
MC/DC100%2/2
$self->validated and not $self->no_update
$self->validated not $self->no_update
Truth table100%3/3
A and not(B)
A= $self->validatedB= not $self->no_update
ABresult
0X0
100
111
413 284 ▶ $self->dump_fields if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 284 284
414 284 $self->processed(1);
415 284 return $self->validated;
416 }
417  
418 sub run {
419 12 ▶ my $self = shift;
Subroutine run: called CC=4 SCAR=13.9
Pod undocumented
420 12 $self->setup_form(@_);
421 12 ▶ $self->validate_form if $self->posted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->posted 8 4 12
422 12 ▶ $self->update_model if ( $self->validated && !$self->no_update );;
Branch: 1 branch
BranchTrueFalseTotal
if $self->validated and not $self->no_update 5 7 12
Condition66%2/3
$self->validated and not $self->no_update
!ll&&!rl&&r
705
MC/DC50%1/2
$self->validated and not $self->no_update
$self->validated not $self->no_update
Truth table66%2/3
A and not(B)
A= $self->validatedB= not $self->no_update
ABresult
0X0
100
111
423 12 my $result = $self->result;
424 12 $self->clear;
425 12 return $result;
426 }
427  
428 sub after_update_model {
429 97 ▶ my $self = shift;
Subroutine after_update_model: called CC=13 SCAR=25.8
Pod undocumented
430 # This an attempt to reload the repeatable
431 # relationships after the database is updated, so that we get the
432 # primary keys of the repeatable elements. Otherwise, if a form
433 # is re-presented, repeatable elements without primary keys may
434 # be created again. There is no reliable way to connect up
435 # existing repeatable elements with their db-created primary keys.
436 97 ▶ if ( $self->has_repeatable_fields && $self->item ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_repeatable_fields and $self->item) 2 95 97
Condition100%3/3
$self->has_repeatable_fields and $self->item
!ll&&!rl&&r
72232
MC/DC100%2/2
$self->has_repeatable_fields and $self->item
$self->has_repeatable_fields $self->item
Truth table100%3/3
A and B
A= $self->has_repeatable_fieldsB= $self->item
ABresult
0X0
100
111
437 2 foreach my $field ( $self->all_repeatable_fields ) {
438 4 ▶ next unless $field->is_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_active 0 4 4
439 # Check to see if there are any repeatable subfields with
440 # null primary keys, so we can skip reloading for the case
441 # where all repeatables have primary keys.
442 4 my $needs_reload = 0;
443 4 foreach my $sub_field ( $field->fields ) {
444 9 ▶ if ( $sub_field->has_flag('is_compound') && $sub_field->has_primary_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_field->has_flag("is_compound") and $sub_field->has_primary_key) 1 8 9
Condition100%3/3
$sub_field->has_flag("is_compound") and $sub_field->has_primary_key
!ll&&!rl&&r
351
MC/DC100%2/2
$sub_field->has_flag("is_compound") and $sub_field->has_primary_key
$sub_field->has_flag("is_compound") $sub_field->has_primary_key
Truth table100%3/3
A and B
A= $sub_field->has_flag("is_compound")B= $sub_field->has_primary_key
ABresult
0X0
100
111
445 1 foreach my $pk_field ( @{ $sub_field->primary_key } ) {
446 1 ▶ $needs_reload++ unless $pk_field->fif;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk_field->fif 1 0 1
447 }
448 1 ▶ last if $needs_reload;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_reload 1 0 1
449 }
450 }
451 4 ▶ next unless $needs_reload;
Branch: 1 branch
BranchTrueFalseTotal
unless $needs_reload 3 1 4
452 1 my @names = split( /\./, $field->full_name );
453 1 my $rep_item = $self->find_sub_item( $self->item, \@names );
454 # $rep_item is a single row or an array of rows or undef
455 # If we found a database item for the repeatable, replace
456 # the existing result with a result derived from the item.
457 1 ▶ if ( ref $rep_item ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rep_item) 1 0 1
458 1 my $parent = $field->parent;
459 1 my $result = $field->result;
460 1 $field->init_state;
461 1 my $new_result = $field->_result_from_object( $result, $rep_item );
462 # find index of existing result
463 1 ▶ my $index = $parent->result->find_result_index( sub { $_ == $result } );
Subroutine __ANON__: called CC=1 SCAR=0.0
464 # replace existing result with new result
465 1 $parent->result->set_result_at_index( $index, $new_result );
466 }
467 }
468 }
469 }
470  
471  
472 sub db_validate {
473 0 ▶ my $self = shift;
Subroutine db_validate: not called CC=1 SCAR=6.9
Pod undocumented
474 0 my $fif = $self->fif;
475 0 $self->process($fif);
476 0 return $self->validated;
477 }
478  
479 sub clear {
480 117 ▶ my $self = shift;
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
481 117 $self->clear_data;
482 117 $self->clear_params;
483 117 $self->clear_posted;
484 117 $self->clear_item;
485 117 $self->clear_init_object;
486 117 $self->clear_ctx;
487 117 $self->processed(0);
488 117 $self->did_init_obj(0);
489 117 $self->clear_result;
490 117 $self->clear_use_defaults_over_obj;
491 117 $self->clear_use_init_obj_over_item;
492 117 $self->clear_no_update;
493 117 $self->clear_info_message;
494 117 $self->clear_for_js;
495 }
496  
497 13 ▶sub values { shift->value }
Subroutine values: called CC=1 SCAR=0.0
Pod undocumented
498  
499 # deprecated?
500 sub error_field_names {
501 1 ▶ my $self = shift;
Subroutine error_field_names: called CC=1 SCAR=0.0
Pod undocumented
502 1 my @error_fields = $self->error_fields;
503 1 return map { $_->name } @error_fields;
504 }
505  
506 sub errors {
507 8 ▶ my $self = shift;
Subroutine errors: called CC=1 SCAR=0.0
Pod documented
508 8 my @error_fields = $self->error_fields;
509 8 my @errors = $self->all_form_errors;
510 8 push @errors, map { $_->all_errors } @error_fields;
511 8 return @errors;
512 }
513  
514 sub errors_by_id {
515 1 ▶ my $self = shift;
Subroutine errors_by_id: called CC=2 SCAR=6.9
Pod undocumented
516 1 my %errors;
517 1 $errors{$_->id} = [$_->all_errors] for $self->error_fields;
518 1 return \%errors;
519 }
520  
521 sub errors_by_name {
522 1 ▶ my $self = shift;
Subroutine errors_by_name: called CC=2 SCAR=6.9
Pod undocumented
523 1 my %errors;
524 1 $errors{$_->html_name} = [$_->all_errors] for $self->error_fields;
525 1 return \%errors;
526 }
527  
528 sub build_errors {
529 177 ▶ my $self = shift;
Subroutine build_errors: called CC=2 SCAR=6.9
Pod undocumented
530 # this puts the errors in the result
531 177 foreach my $err_res (@{$self->result->error_results}) {
532 103 $self->result->_push_errors($err_res->all_errors);
533 }
534 }
535  
536 sub uuid {
537 0 ▶ my $form = shift;
Subroutine uuid: not called CC=1 SCAR=6.9
Pod undocumented
538 0 require Data::UUID;
539 0 my $uuid = Data::UUID->new->create_str;
540 0 return qq[<input type="hidden" name="form_uuid" value="$uuid">];
541 }
542  
543 sub validate_form {
544 177 ▶ my $self = shift;
Subroutine validate_form: called CC=2 SCAR=6.9
Pod undocumented
545 177 my $params = $self->params;
546 177 $self->_set_dependency; # set required dependencies
547 177 $self->_fields_validate;
548 177 $self->validate; # empty method for users
549 177 $self->validate_model; # model specific validation
550 177 $self->fields_set_value;
551 177 $self->build_errors; # move errors to result
552 177 $self->_clear_dependency;
553 177 $self->clear_posted;
554 177 $self->ran_validation(1);
555 177 ▶ $self->dump_validated if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 177 177
556 177 return $self->validated;
557 }
558  
559 170 ▶sub validate { 1 }
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
560  
561 sub has_errors {
562 20 ▶ my $self = shift;
Subroutine has_errors: called CC=2 SCAR=6.9
Pod undocumented
563 20 ▶ return $self->has_error_fields || $self->has_form_errors;
Condition100%3/3
$self->has_error_fields || $self->has_form_errors
l!l&&r!l&&!r
1244
MC/DC100%2/2
$self->has_error_fields || $self->has_form_errors
$self->has_error_fields $self->has_form_errors
Truth table100%3/3
A || B
A= $self->has_error_fieldsB= $self->has_form_errors
ABresult
1X1
011
000
564 }
565 sub num_errors {
566 6 ▶ my $self = shift;
Subroutine num_errors: called CC=1 SCAR=0.0
Pod undocumented
567 6 return $self->num_error_fields + $self->num_form_errors;
568 }
569 0 ▶sub get_errors { shift->errors }
Subroutine get_errors: not called CC=1 SCAR=6.9
Pod undocumented
570  
571 sub setup_form {
572 296 ▶ my ( $self, @args ) = @_;
Subroutine setup_form: called CC=16 SCAR=28.0
Pod undocumented
573 296 ▶ if ( @args == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 80 216 296
elsif (@args > 1) { } 181 35 216
574 80 $self->params( $args[0] );
575 }
576 elsif ( @args > 1 ) {
577 181 my $hashref = {@args};
578 181 ▶ while ( my ( $key, $value ) = each %{$hashref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$hashref;}) 251 181 432
579 251 ▶ confess "invalid attribute '$key' passed to setup_form"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($key) 0 251 251
580 unless $self->can($key);
581 251 $self->$key($value);
582 }
583 }
584 296 ▶ if ( $self->item_id && !$self->item ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->item_id and not $self->item) 0 296 296
Condition66%2/3
$self->item_id and not $self->item
!ll&&!rl&&r
29420
MC/DC0%0/2
$self->item_id and not $self->item
$self->item_id not $self->item
Truth table66%2/3
A and not(B)
A= $self->item_idB= not $self->item
ABresult
0X0
100
111
585 0 $self->item( $self->build_item );
586 }
587 296 $self->clear_result;
588 296 $self->set_active;
589 296 $self->update_fields;
590 # initialization of Repeatable fields and Select options
591 # will be done in _result_from_object when there's an initial object
592 # in _result_from_input when there are params
593 # and by _result_from_fields for empty forms
594 296 ▶ $self->posted(1) if ( $self->has_params && !$self->has_posted );
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_params and not $self->has_posted 176 120 296
Condition100%3/3
$self->has_params and not $self->has_posted
!ll&&!rl&&r
1191176
MC/DC100%2/2
$self->has_params and not $self->has_posted
$self->has_params not $self->has_posted
Truth table100%3/3
A and not(B)
A= $self->has_paramsB= not $self->has_posted
ABresult
0X0
100
111
595 296 ▶ if ( !$self->did_init_obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->did_init_obj) 296 0 296
596 296 ▶ if ( my $init_object = $self->use_init_obj_over_item ?
Branch: 3 branches
BranchTrueFalseTotal
if (my $init_object = $self->use_init_obj_over_item ? $self->init_object || $self->item : $self->item || $self->init_object) { } 55 241 296
elsif (not $self->posted) { } 75 166 241
$self->use_init_obj_over_item ? : 1 295 296
Condition66%4/6
$self->init_object or $self->item
l!l&&r!l&&!r
100
$self->item or $self->init_object
l!l&&r!l&&!r
1143241
MC/DC0%0/2
$self->init_object or $self->item
$self->init_object $self->item
MC/DC100%2/2
$self->item or $self->init_object
$self->item $self->init_object
Truth table33%1/3
A or B
A= $self->init_objectB= $self->item
ABresult
1X1
011
000
Truth table100%3/3
A or B
A= $self->itemB= $self->init_object
ABresult
1X1
011
000
597 ($self->init_object || $self->item) : ( $self->item || $self->init_object ) ) {
598 55 $self->_result_from_object( $self->result, $init_object );
599 }
600 elsif ( !$self->posted ) {
601 # no initial object. empty form must be initialized
602 75 $self->_result_from_fields( $self->result );
603 }
604 }
605 # if params exist and if posted flag is either not set or set to true
606 296 my $params = clone( $self->params );
607 296 ▶ if ( $self->posted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->posted) 177 119 296
608 177 $self->clear_result;
609 177 $self->_result_from_input( $self->result, $params, 1 );
610 }
611  
612 }
613  
614 # if active => [...] is set at process time, set 'active' flag
615 sub set_active {
616 292 ▶ my $self = shift;
Subroutine set_active: called CC=7 SCAR=19.7
Pod undocumented
617 292 ▶ if( $self->has_active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_active) 1 291 292
618 1 foreach my $fname (@{$self->active}) {
619 1 my $field = $self->field($fname);
620 1 ▶ if ( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 1 0 1
621 1 $field->_active(1);
622 }
623 else {
624 0 warn "field $fname not found to set active";
625 }
626 }
627 1 $self->clear_active;
628 }
629 292 ▶ if( $self->has_inactive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_inactive) 1 291 292
630 1 foreach my $fname (@{$self->inactive}) {
631 1 my $field = $self->field($fname);
632 1 ▶ if ( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 1 0 1
633 1 $field->_active(0);
634 }
635 else {
636 0 warn "field $fname not found to set inactive";
637 }
638 }
639 1 $self->clear_inactive;
640 }
641 }
642  
643 # if active => [...] is set at build time, remove 'inactive' flags
644 sub build_active {
645 1 ▶ my $self = shift;
Subroutine build_active: called CC=7 SCAR=26.3
Pod undocumented
646 1 ▶ if( $self->has_active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_active) 0 1 1
647 0 foreach my $fname (@{$self->active}) {
648 0 my $field = $self->field($fname);
649 0 ▶ if( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 0 0 0
650 0 $field->clear_inactive;
651 }
652 else {
653 0 warn "field $fname not found to set active";
654 }
655 }
656 0 $self->clear_active;
657 }
658 1 ▶ if( $self->has_inactive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_inactive) 1 0 1
659 1 foreach my $fname (@{$self->inactive}) {
660 1 my $field = $self->field($fname);
661 1 ▶ if( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 1 0 1
662 1 $field->inactive(1);
663 }
664 else {
665 0 warn "field $fname not found to set inactive";
666 }
667 }
668 1 $self->clear_inactive;
669 }
670 }
671  
672 76 ▶sub fif { shift->fields_fif(@_) }
Subroutine fif: called CC=1 SCAR=0.0
Pod documented
673  
674 # this is subclassed by the model, which may
675 # do a lot more than this
676 sub init_value {
677 257 ▶ my ( $self, $field, $value ) = @_;
Subroutine init_value: called CC=1 SCAR=0.0
Pod undocumented
678 257 $field->init_value($value);
679 257 $field->_set_value($value);
680 }
681  
682 sub _set_dependency {
683 177 ▶ my $self = shift;
Subroutine _set_dependency: called CC=12 SCAR=26.2
684  
685 177 ▶ my $depends = $self->dependency || return;
Condition100%2/2
$self->dependency || (return)
l!l
9168
MC/DC100%1/1
$self->dependency || (return)
$self->dependency
Truth table100%2/2
A
A= $self->dependency
Aresult
11
00
686 9 my $params = $self->params;
687 9 for my $group (@$depends) {
688 10 ▶ next if @$group < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$group < 2 0 10 10
689 # process a group of fields
690 10 for my $name (@$group) {
691 # is there a value?
692 27 my $value = $params->{$name};
693 27 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 25 2 27
694 # The exception is a boolean can be zero which we count as not set.
695 # This is to allow requiring a field when a boolean is true.
696 2 my $field = $self->field($name);
697 2 ▶ next if $self->field($name)->type eq 'Boolean' && $value == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->field($name)->type eq "Boolean" and $value == 0 0 2 2
Condition33%1/3
$self->field($name)->type eq "Boolean" and $value == 0
!ll&&!rl&&r
200
MC/DC0%0/2
$self->field($name)->type eq "Boolean" and $value == 0
$self->field($name)->type eq "Boolean" $value == 0
Truth table33%1/3
A and B
A= $self->field($name)->type eq "Boolean"B= $value == 0
ABresult
0X0
100
111
698 2 ▶ next unless HTML::FormHandler::Field::has_some_value($value);
Branch: 1 branch
BranchTrueFalseTotal
unless HTML::FormHandler::Field::has_some_value($value) 0 2 2
699 # one field was found non-blank, so set all to required
700 2 for (@$group) {
701 6 my $field = $self->field($_);
702 6 ▶ next unless $field && !$field->required;
Branch: 1 branch
BranchTrueFalseTotal
unless $field and not $field->required 0 6 6
Condition33%1/3
$field and not $field->required
!ll&&!rl&&r
006
MC/DC0%0/2
$field and not $field->required
$field not $field->required
Truth table33%1/3
A and not(B)
A= $fieldB= not $field->required
ABresult
0X0
100
111
703 6 $self->add_required($field); # save for clearing later.
704 6 $field->required(1);
705 }
706 2 last;
707 }
708 }
709 }
710  
711 sub _clear_dependency {
712 177 ▶ my $self = shift;
Subroutine _clear_dependency: called CC=2 SCAR=6.9
713  
714 177 $_->required(0) for @{$self->_required};
715 177 $self->clear_required;
716 }
717  
718 sub peek {
719 0 ▶ my $self = shift;
Subroutine peek: not called CC=2 SCAR=17.9
Pod documented
720 0 my $string = "Form " . $self->name . "\n";
721 0 my $indent = ' ';
722 0 foreach my $field ( $self->sorted_fields ) {
723 0 $string .= $field->peek( $indent );
724 }
725 0 return $string;
726 }
727  
728 sub _munge_params {
729 367 ▶ my ( $self, $params, $attr ) = @_;
Subroutine _munge_params: called CC=4 SCAR=13.9
730 367 ▶ my $_fix_params = $self->params_class->new( @{ $self->params_args || [] } );
Branch: 1 branch
BranchTrueFalseTotal
$self->params_args or [] 367 0 367
731 367 my $new_params = $_fix_params->expand_hash($params);
732 367 ▶ if ( $self->html_prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->html_prefix) 6 361 367
733 6 $new_params = $new_params->{ $self->name };
734 }
735 367 ▶ $new_params = {} if !defined $new_params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_params 4 363 367
736 367 $self->{params} = $new_params;
737 }
738  
739 sub params_to_values {
740 1 ▶ my ( $self, $params ) = @_;
Subroutine params_to_values: called CC=2 SCAR=7.0
Pod undocumented
741 1 ▶ my $_fix_params = $self->params_class->new( @{ $self->params_args || [] } );
Branch: 1 branch
BranchTrueFalseTotal
$self->params_args or [] 1 0 1
742 1 my $new_params = $_fix_params->expand_hash($params);
743 1 return $new_params;
744 }
745  
746 sub add_form_error {
747 4 ▶ my ( $self, @message ) = @_;
Subroutine add_form_error: called CC=2 SCAR=7.3
Pod undocumented
748  
749 4 ▶ unless ( defined $message[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $message[0]) 0 4 4
750 0 @message = ('form is invalid');
751 }
752 4 my $out;
753 try {
754 4 ▶ $out = $self->_localize(@message);
Subroutine __ANON__: called CC=1 SCAR=0.0
755 }
756 catch {
757 0 ▶ die "Error occurred localizing error message for " . $self->name . ". $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
758 4 };
759 4 $self->push_form_errors($out);
760 4 return;
761 }
762  
763 ▶sub get_default_value { }
Subroutine get_default_value: called CC=1 SCAR=0.0
Pod undocumented
764 ▶sub _can_deflate { }
Subroutine _can_deflate: not called CC=1 SCAR=0.0
765  
766 sub update_fields {
767 294 ▶ my $self = shift;
Subroutine update_fields: called CC=5 SCAR=16.1
Pod undocumented
768 294 ▶ if( $self->has_update_field_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_update_field_list) 4 290 294
769 4 my $updates = $self->update_field_list;
770 4 foreach my $field_name ( keys %{$updates} ) {
771 6 $self->update_field($field_name, $updates->{$field_name} );
772 }
773 4 $self->clear_update_field_list;
774 }
775 294 ▶ if( $self->has_defaults ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_defaults) 2 292 294
776 2 my $defaults = $self->defaults;
777 2 foreach my $field_name ( keys %{$defaults} ) {
778 5 $self->update_field($field_name, { default => $defaults->{$field_name} } );
779 }
780 2 $self->clear_defaults;
781 }
782 }
783  
784 sub update_field {
785 11 ▶ my ( $self, $field_name, $updates ) = @_;
Subroutine update_field: called CC=5 SCAR=16.3
Pod undocumented
786  
787 11 my $field = $self->field($field_name);
788 11 ▶ unless( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 11 11
789 0 die "Field $field_name is not found and cannot be updated by update_fields";
790 }
791 11 ▶ while ( my ( $attr_name, $attr_value ) = each %{$updates} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$updates;}) 13 11 24
792 13 ▶ confess "invalid attribute '$attr_name' passed to update_field"
Branch: 1 branch
BranchTrueFalseTotal
unless $field->can($attr_name) 0 13 13
793 unless $field->can($attr_name);
794 13 ▶ if( $attr_name eq 'tags' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_name eq 'tags') { } 1 12 13
795 1 $field->set_tag(%$attr_value);
796 }
797 else {
798 12 $field->$attr_name($attr_value);
799 }
800 }
801 }
802  
803  
804 146 __PACKAGE__->meta->make_immutable;
805 146 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
806 146 1;
807  
808 __END__
809  
810 =pod
811  
812 =encoding UTF-8
813  
814 =head1 NAME
815  
816 HTML::FormHandler - HTML forms using Moose
817  
818 =head1 VERSION
819  
820 version 0.410001
821  
822 =head1 SYNOPSIS
823  
824 See the manual at L<HTML::FormHandler::Manual>.
825  
826 use HTML::FormHandler; # or a custom form: use MyApp::Form::User;
827 my $form = HTML::FormHandler->new( .... );
828 $form->process( params => $params );
829 my $rendered_form = $form->render;
830 if( $form->validated ) {
831 # perform validated form actions
832 }
833 else {
834 # perform non-validated actions
835 }
836  
837 Or, if you want to use a form 'result' (which contains only the form
838 values and error messages) instead:
839  
840 use MyApp::Form; # or a generic form: use HTML::FormHandler;
841 my $form = MyApp::Form->new( .... );
842 my $result = $form->run( params => $params );
843 if( $result->validated ) {
844 # perform validated form actions
845 }
846 else {
847 # perform non-validated actions
848 $result->render;
849 }
850  
851 An example of a custom form class:
852  
853 package MyApp::Form::User;
854  
855 use HTML::FormHandler::Moose;
856 extends 'HTML::FormHandler';
857 use Moose::Util::TypeConstraints;
858  
859 has '+item_class' => ( default => 'User' );
860  
861 has_field 'name' => ( type => 'Text' );
862 has_field 'age' => ( type => 'PosInteger', apply => [ 'MinimumAge' ] );
863 has_field 'birthdate' => ( type => 'DateTime' );
864 has_field 'birthdate.month' => ( type => 'Month' );
865 has_field 'birthdate.day' => ( type => 'MonthDay' );
866 has_field 'birthdate.year' => ( type => 'Year' );
867 has_field 'hobbies' => ( type => 'Multiple' );
868 has_field 'address' => ( type => 'Text' );
869 has_field 'city' => ( type => 'Text' );
870 has_field 'state' => ( type => 'Select' );
871 has_field 'email' => ( type => 'Email' );
872  
873 has '+dependency' => ( default => sub {
874 [ ['address', 'city', 'state'], ]
875 }
876 );
877  
878 subtype 'MinimumAge'
879 => as 'Int'
880 => where { $_ > 13 }
881 => message { "You are not old enough to register" };
882  
883 no HTML::FormHandler::Moose;
884 1;
885  
886 A dynamic form - one that does not use a custom form class - may be
887 created using the 'field_list' attribute to set fields:
888  
889 my $form = HTML::FormHandler->new(
890 name => 'user_form',
891 item => $user,
892 field_list => [
893 'username' => {
894 type => 'Text',
895 apply => [ { check => qr/^[0-9a-z]*\z/,
896 message => 'Contains invalid characters' } ],
897 },
898 'select_bar' => {
899 type => 'Select',
900 options => \@select_options,
901 multiple => 1,
902 size => 4,
903 },
904 ],
905 );
906  
907 FormHandler does not provide a custom controller for Catalyst because
908 it isn't necessary. Interfacing to FormHandler is only a couple of
909 lines of code. See L<HTML::FormHandler::Manual::Catalyst> for more
910 details, or L<Catalyst::Manual::Tutorial::09_AdvancedCRUD::09_FormHandler>.
911  
912 =head1 DESCRIPTION
913  
914 *** Although documentation in this file provides some overview, it is mainly
915 intended for API documentation. See L<HTML::FormHandler::Manual::Intro>
916 for an introduction, with links to other documentation.
917  
918 HTML::FormHandler maintains a clean separation between form construction
919 and form rendering. It allows you to define your forms and fields in a
920 number of flexible ways. Although it provides renderers for HTML, you
921 can define custom renderers for any kind of presentation.
922  
923 HTML::FormHandler allows you to define form fields and validators. It can
924 be used for both database and non-database forms, and will
925 automatically update or create rows in a database. It can be used
926 to process structured data that doesn't come from an HTML form.
927  
928 One of its goals is to keep the controller/application program interface as
929 simple as possible, and to minimize the duplication of code. In most cases,
930 interfacing your controller to your form is only a few lines of code.
931  
932 With FormHandler you shouldn't have to spend hours trying to figure out how to make a
933 simple HTML change that would take one minute by hand. Because you _can_ do it
934 by hand. Or you can automate HTML generation as much as you want, with
935 template widgets or pure Perl rendering classes, and stay completely in
936 control of what, where, and how much is done automatically. You can define
937 custom renderers and display your rendered forms however you want.
938  
939 You can split the pieces of your forms up into logical parts and compose
940 complete forms from FormHandler classes, roles, fields, collections of
941 validations, transformations and Moose type constraints.
942 You can write custom methods to process forms, add any attribute you like,
943 and use Moose method modifiers. FormHandler forms are Perl classes, so there's
944 a lot of flexibility in what you can do.
945  
946 HTML::FormHandler provides rendering through roles which are applied to
947 form and field classes (although there's no reason you couldn't write
948 a renderer as an external object either). There are currently two flavors:
949 all-in-one solutions like L<HTML::FormHandler::Render::Simple> and
950 L<HTML::FormHandler::Render::Table> that contain methods for rendering
951 field widget classes, and the L<HTML::FormHandler::Widget> roles, which are
952 more atomic roles which are automatically applied to fields and form. See
953 L<HTML::FormHandler::Manual::Rendering> for more details.
954 (And you can easily use hand-built forms - FormHandler doesn't care.)
955  
956 The typical application for FormHandler would be in a Catalyst, DBIx::Class,
957 Template Toolkit web application, but use is not limited to that. FormHandler
958 can be used in any Perl application.
959  
960 More Formhandler documentation and a tutorial can be found in the manual
961 at L<HTML::FormHandler::Manual>.
962  
963 =head1 STARTING A NEW PROJECT?
964  
965 Consider using L<Data::MuForm> instead. It is the successor to this module
966 and may provide you speed or usability benefits. See
967 L<Data::MuForm::Manual::FormHandlerDiff> for differences between HFH and DMF.
968  
969 =head1 ATTRIBUTES and METHODS
970  
971 =head2 Creating a form with 'new'
972  
973 The new constructor takes name/value pairs:
974  
975 MyForm->new(
976 item => $item,
977 );
978  
979 No attributes are required on new. The form's fields will be built from
980 the form definitions. If no initial data object or defaults have been provided, the form
981 will be empty. Most attributes can be set on either 'new' or 'process'.
982 The common attributes to be passed in to the constructor for a database form
983 are either item_id and schema or item:
984  
985 item_id - database row primary key
986 item - database row object
987 schema - (for DBIC) the DBIx::Class schema
988  
989 The following are sometimes passed in, but are also often set
990 in the form class:
991  
992 item_class - source name of row
993 dependency - (see dependency)
994 field_list - an array of field definitions
995 init_object - a hashref or object to provide initial values
996  
997 Examples of creating a form object with new:
998  
999 my $form = MyApp::Form::User->new;
1000  
1001 # database form using a row object
1002 my $form = MyApp::Form::Member->new( item => $row );
1003  
1004 # a dynamic form (no form class has been defined)
1005 my $form = HTML::FormHandler::Model::DBIC->new(
1006 item_id => $id,
1007 item_class => 'User',
1008 schema => $schema,
1009 field_list => [
1010 name => 'Text',
1011 active => 'Boolean',
1012 submit_btn => 'Submit',
1013 ],
1014 );
1015  
1016 See the model class for more information about 'item', 'item_id',
1017 'item_class', and 'schema' (for the DBIC model).
1018 L<HTML::FormHandler::Model::DBIC>.
1019  
1020 FormHandler forms are handled in two steps: 1) create with 'new',
1021 2) handle with 'process'. FormHandler doesn't
1022 care whether most parameters are set on new or process or update,
1023 but a 'field_list' argument must be passed in on 'new' since the
1024 fields are built at construction time.
1025  
1026 If you want to update field attributes on the 'process' call, you can
1027 use an 'update_field_list' or 'defaults' hashref attribute , or subclass
1028 update_fields in your form. The 'update_field_list' hashref can be used
1029 to set any field attribute. The 'defaults' hashref will update only
1030 the 'default' attribute in the field. (There are a lot of ways to
1031 set defaults. See L<HTML::FormHandler::Manual::Defaults>.)
1032  
1033 $form->process( defaults => { foo => 'foo_def', bar => 'bar_def' } );
1034 $form->process( update_field_list => { foo => { label => 'New Label' } });
1035  
1036 Field results are built on the 'new' call, but will then be re-built
1037 on the process call. If you always use 'process' before rendering the form,
1038 accessing fields, etc, you can set the 'no_preload' flag to skip this step.
1039  
1040 =head2 Processing the form
1041  
1042 =head3 process
1043  
1044 Call the 'process' method on your form to perform validation and
1045 update. A database form must have either an item (row object) or
1046 a schema, item_id (row primary key), and item_class (usually set in the form).
1047 A non-database form requires only parameters.
1048  
1049 $form->process( item => $book, params => $c->req->parameters );
1050 $form->process( item_id => $item_id,
1051 schema => $schema, params => $c->req->parameters );
1052 $form->process( params => $c->req->parameters );
1053  
1054 This process method returns the 'validated' flag (C<< $form->validated >>).
1055 If it is a database form and the form validates, the database row
1056 will be updated.
1057  
1058 After the form has been processed, you can get a parameter hashref suitable
1059 for using to fill in the form from C<< $form->fif >>.
1060 A hash of inflated values (that would be used to update the database for
1061 a database form) can be retrieved with C<< $form->value >>.
1062  
1063 If you don't want to update the database on this process call, you can
1064 set the 'no_update' flag:
1065  
1066 $form->process( item => $book, params => $params, no_update => 1 );
1067  
1068 =head3 params
1069  
1070 Parameters are passed in when you call 'process'.
1071 HFH gets data to validate and store in the database from the params hash.
1072 If the params hash is empty, no validation is done, so it is not necessary
1073 to check for POST before calling C<< $form->process >>. (Although see
1074 the 'posted' option for complications.)
1075  
1076 Params can either be in the form of CGI/HTTP style params:
1077  
1078 {
1079 user_name => "Joe Smith",
1080 occupation => "Programmer",
1081 'addresses.0.street' => "999 Main Street",
1082 'addresses.0.city' => "Podunk",
1083 'addresses.0.country' => "UT",
1084 'addresses.0.address_id' => "1",
1085 'addresses.1.street' => "333 Valencia Street",
1086 'addresses.1.city' => "San Francisco",
1087 'addresses.1.country' => "UT",
1088 'addresses.1.address_id' => "2",
1089 }
1090  
1091 or as structured data in the form of hashes and lists:
1092  
1093 {
1094 addresses => [
1095 {
1096 city => 'Middle City',
1097 country => 'GK',
1098 address_id => 1,
1099 street => '101 Main St',
1100 },
1101 {
1102 city => 'DownTown',
1103 country => 'UT',
1104 address_id => 2,
1105 street => '99 Elm St',
1106 },
1107 ],
1108 'occupation' => 'management',
1109 'user_name' => 'jdoe',
1110 }
1111  
1112 CGI style parameters will be converted to hashes and lists for HFH to
1113 operate on.
1114  
1115 =head3 posted
1116  
1117 Note that FormHandler by default uses empty params as a signal that the
1118 form has not actually been posted, and so will not attempt to validate
1119 a form with empty params. Most of the time this works OK, but if you
1120 have a small form with only the controls that do not return a post
1121 parameter if unselected (checkboxes and select lists), then the form
1122 will not be validated if everything is unselected. For this case you
1123 can either add a hidden field as an 'indicator', or use the 'posted' flag:
1124  
1125 $form->process( posted => ($c->req->method eq 'POST'), params => ... );
1126  
1127 The 'posted' flag also works to prevent validation from being performed
1128 if there are extra params in the params hash and it is not a 'POST' request.
1129  
1130 =head2 Getting data out
1131  
1132 =head3 fif (fill in form)
1133  
1134 If you don't use FormHandler rendering and want to fill your form values in
1135 using some other method (such as with HTML::FillInForm or using a template)
1136 this returns a hash of values that are equivalent to params which you may
1137 use to fill in your form.
1138  
1139 The fif value for a 'title' field in a TT form:
1140  
1141 [% form.fif.title %]
1142  
1143 Or you can use the 'fif' method on individual fields:
1144  
1145 [% form.field('title').fif %]
1146  
1147 If you use FormHandler to render your forms or field you probably won't use
1148 these methods.
1149  
1150 =head3 value
1151  
1152 Returns a hashref of all field values. Useful for non-database forms, or if
1153 you want to update the database yourself. The 'fif' method returns
1154 a hashref with the field names for the keys and the field's 'fif' for the
1155 values; 'value' returns a hashref with the field accessors for the keys, and the
1156 field's 'value' (possibly inflated) for the values.
1157  
1158 Forms containing arrays to be processed with L<HTML::FormHandler::Field::Repeatable>
1159 will have parameters with dots and numbers, like 'addresses.0.city', while the
1160 values hash will transform the fields with numbers to arrays.
1161  
1162 =head2 Accessing and setting up fields
1163  
1164 Fields are declared with a number of attributes which are defined in
1165 L<HTML::FormHandler::Field>. If you want additional attributes you can
1166 define your own field classes (or apply a role to a field class - see
1167 L<HTML::FormHandler::Manual::Cookbook>). The field 'type' (used in field
1168 definitions) is the short class name of the field class, used when
1169 searching the 'field_name_space' for the field class.
1170  
1171 =head3 has_field
1172  
1173 The most common way of declaring fields is the 'has_field' syntax.
1174 Using the 'has_field' syntax sugar requires C< use HTML::FormHandler::Moose; >
1175 or C< use HTML::FormHandler::Moose::Role; > in a role.
1176 See L<HTML::FormHandler::Manual::Intro>
1177  
1178 use HTML::FormHandler::Moose;
1179 has_field 'field_name' => ( type => 'FieldClass', .... );
1180  
1181 =head3 field_list
1182  
1183 A 'field_list' is an array of field definitions which can be used as an
1184 alternative to 'has_field' in small, dynamic forms to create fields.
1185  
1186 field_list => [
1187 field_one => {
1188 type => 'Text',
1189 required => 1
1190 },
1191 field_two => 'Text,
1192 ]
1193  
1194 The field_list array takes elements which are either a field_name key
1195 pointing to a 'type' string or a field_name key pointing to a
1196 hashref of field attributes. You can also provide an array of
1197 hashref elements with the name as an additional attribute.
1198 The field list can be set inside a form class, when you want to
1199 add fields to the form depending on some other state, although
1200 you can also create all the fields and set some of them inactive.
1201  
1202 sub field_list {
1203 my $self = shift;
1204 my $fields = $self->schema->resultset('SomeTable')->
1205 search({user_id => $self->user_id, .... });
1206 my @field_list;
1207 while ( my $field = $fields->next )
1208 {
1209 < create field list >
1210 }
1211 return \@field_list;
1212 }
1213  
1214 =head3 update_field_list
1215  
1216 Used to dynamically set particular field attributes on the 'process' (or
1217 'run') call. (Will not create fields.)
1218  
1219 $form->process( update_field_list => {
1220 foo_date => { format => '%m/%e/%Y', date_start => '10-01-01' } },
1221 params => $params );
1222  
1223 The 'update_field_list' is processed by the 'update_fields' form method,
1224 which can also be used in a form to do specific field updates:
1225  
1226 sub update_fields {
1227 my $self = shift;
1228 $self->field('foo')->temp( 'foo_temp' );
1229 $self->field('bar')->default( 'foo_value' );
1230 $self->next::method();
1231 }
1232  
1233 (Note that you although you can set a field's 'default', you can't set a
1234 field's 'value' directly here, since it will
1235 be overwritten by the validation process. Set the value in a field
1236 validation method.)
1237  
1238 =head3 update_subfields
1239  
1240 Yet another way to provide settings for the field, except this one is intended for
1241 use in roles and compound fields, and is only executed when the form is
1242 initially built. It takes the same field name keys as 'update_field_list', plus
1243 'all', 'by_flag', and 'by_type'.
1244  
1245 sub build_update_subfields {{
1246 all => { tags => { wrapper_tag => 'p' } },
1247 foo => { element_class => 'blue' },
1248 }}
1249  
1250 The 'all' hash key will apply updates to all fields. (Conflicting attributes
1251 in a field definition take precedence.)
1252  
1253 The 'by_flag' hash key will apply updates to fields with a particular flag.
1254 The currently supported subkeys are 'compound', 'contains', and 'repeatable'.
1255 (For repeatable instances, in addition to 'contains' you can also use the
1256 'repeatable' key and the 'init_contains' attribute.)
1257 This is useful for turning on the rendering
1258 wrappers for compounds and repeatables, which are off by default. (The
1259 repeatable instances are wrapped by default.)
1260  
1261 sub build_update_subfields {{
1262 by_flag => { compound => { do_wrapper => 1 } },
1263 by_type => { Select => { element_class => ['sel_elem'] } },
1264 }}
1265  
1266 The 'by_type' hash key will provide values to all fields of a particular
1267 type.
1268  
1269 =head3 defaults
1270  
1271 This is a more specialized version of the 'update_field_list'. It can be
1272 used to provide 'default' settings for fields, in a shorthand way (you don't
1273 have to say 'default' for every field).
1274  
1275 $form->process( defaults => { foo => 'this_foo', bar => 'this_bar' }, ... );
1276  
1277 =head3 active/inactive
1278  
1279 A field can be marked 'inactive' and set to active at new or process time
1280 by specifying the field name in the 'active' array:
1281  
1282 has_field 'foo' => ( type => 'Text', inactive => 1 );
1283 ...
1284 my $form = MyApp::Form->new( active => ['foo'] );
1285 ...
1286 $form->process( active => ['foo'] );
1287  
1288 Or a field can be a normal active field and set to inactive at new or process
1289 time:
1290  
1291 has_field 'bar';
1292 ...
1293 my $form = MyApp::Form->new( inactive => ['foo'] );
1294 ...
1295 $form->process( inactive => ['foo'] );
1296  
1297 Fields specified as active/inactive on new will have the form's inactive/active
1298 arrayref cleared and the field's inactive flag set appropriately, so that
1299 the state will be effective for the life of the form object. Fields specified as
1300 active/inactive on 'process' will have the field's '_active' flag set for the life
1301 of the request (the _active flag will be cleared when the form is cleared).
1302  
1303 The 'sorted_fields' method returns only active fields, sorted according to the
1304 'order' attribute. The 'fields' method returns all fields.
1305  
1306 foreach my $field ( $self->sorted_fields ) { ... }
1307  
1308 You can test whether a field is active by using the field 'is_active' and 'is_inactive'
1309 methods.
1310  
1311 =head3 field_name_space
1312  
1313 Use to look for field during form construction. If a field is not found
1314 with the field_name_space (or HTML::FormHandler/HTML::FormHandlerX),
1315 the 'type' must start with a '+' and be the complete package name.
1316  
1317 =head3 fields
1318  
1319 The array of fields, objects of L<HTML::FormHandler::Field> or its subclasses.
1320 A compound field will itself have an array of fields,
1321 so this is a tree structure.
1322  
1323 =head3 sorted_fields
1324  
1325 Returns those fields from the fields array which are currently active. This
1326 is the method that returns the fields that are looped through when rendering.
1327  
1328 =head3 field($name), subfield($name)
1329  
1330 'field' is the method that is usually called to access a field:
1331  
1332 my $title = $form->field('title')->value;
1333 [% f = form.field('title') %]
1334  
1335 my $city = $form->field('addresses.0.city')->value;
1336  
1337 Pass a second true value to die on errors.
1338  
1339 Since fields are searched for using the form as a base, if you want to find
1340 a sub field in a compound field method, the 'subfield' method may be more
1341 useful, since you can search starting at the current field. The 'chained'
1342 method also works:
1343  
1344 -- in a compound field --
1345 $self->field('media.caption'); # fails
1346 $self->field('media')->field('caption'); # works
1347 $self->subfield('media.caption'); # works
1348  
1349 =head2 Constraints and validation
1350  
1351 Most validation is performed on a per-field basis, and there are a number
1352 of different places in which validation can be performed.
1353  
1354 See also L<HTML::FormHandler::Manual::Validation>.
1355  
1356 =head3 Form class validation for individual fields
1357  
1358 You can define a method in your form class to perform validation on a field.
1359 This method is the equivalent of the field class validate method except it is
1360 in the form class, so you might use this
1361 validation method if you don't want to create a field subclass.
1362  
1363 It has access to the form ($self) and the field.
1364 This method is called after the field class 'validate' method, and is not
1365 called if the value for the field is empty ('', undef). (If you want an
1366 error message when the field is empty, use the 'required' flag and message
1367 or the form 'validate' method.)
1368 The name of this method can be set with 'set_validate' on the field. The
1369 default is 'validate_' plus the field name:
1370  
1371 sub validate_testfield { my ( $self, $field ) = @_; ... }
1372  
1373 If the field name has dots they should be replaced with underscores.
1374  
1375 Note that you can also provide a coderef which will be a method on the field:
1376  
1377 has_field 'foo' => ( validate_method => \&validate_foo );
1378  
1379 =head3 validate
1380  
1381 This is a form method that is useful for cross checking values after they have
1382 been saved as their final validated value, and for performing more complex
1383 dependency validation. It is called after all other field validation is done,
1384 and whether or not validation has succeeded, so it has access to the
1385 post-validation values of all the fields.
1386  
1387 This is the best place to do validation checks that depend on the values of
1388 more than one field.
1389  
1390 =head2 Accessing errors
1391  
1392 Also see L<HTML::FormHandler::Manual::Errors>.
1393  
1394 Set an error in a field with C<< $field->add_error('some error string'); >>.
1395 Set a form error not tied to a specific field with
1396 C<< $self->add_form_error('another error string'); >>.
1397 The 'add_error' and 'add_form_error' methods call localization. If you
1398 want to skip localization for a particular error, you can use 'push_errors'
1399 or 'push_form_errors' instead.
1400  
1401 has_errors - returns true or false
1402 error_fields - returns list of fields with errors
1403 errors - returns array of error messages for the entire form
1404 num_errors - number of errors in form
1405  
1406 Each field has an array of error messages. (errors, has_errors, num_errors,
1407 clear_errors)
1408  
1409 $form->field('title')->errors;
1410  
1411 Compound fields also have an array of error_fields.
1412  
1413 =head2 Clear form state
1414  
1415 The clear method is called at the beginning of 'process' if the form
1416 object is reused, such as when it is persistent in a Moose attribute,
1417 or in tests. If you add other attributes to your form that are set on
1418 each request, you may need to clear those yourself.
1419  
1420 If you do not call the form's 'process' method on a persistent form,
1421 such as in a REST controller's non-POST method, or if you only call
1422 process when the form is posted, you will also need to call C<< $form->clear >>.
1423  
1424 The 'run' method which returns a result object always performs 'clear', to
1425 keep the form object clean.
1426  
1427 =head2 Miscellaneous attributes
1428  
1429 =head3 name
1430  
1431 The form's name. Useful for multiple forms. Used for the form element 'id'.
1432 When 'html_prefix' is set it is used to construct the field 'id'
1433 and 'name'. The default is "form" + a one to three digit random number.
1434 Because the HTML standards have flip-flopped on whether the HTML
1435 form element can contain a 'name' attribute, please set a name attribute
1436 using 'form_element_attr'.
1437  
1438 =head3 init_object
1439  
1440 An 'init_object' may be used instead of the 'item' to pre-populate the values
1441 in the form. This can be useful when populating a form from default values
1442 stored in a similar but different object than the one the form is creating.
1443 The 'init_object' should be either a hash or the same type of object that
1444 the model uses (a DBIx::Class row for the DBIC model). It can be set in a
1445 variety of ways:
1446  
1447 my $form = MyApp::Form->new( init_object => { .... } );
1448 $form->process( init_object => {...}, ... );
1449 has '+init_object' => ( default => sub { { .... } } );
1450 sub init_object { my $self = shift; .... }
1451  
1452 The method version is useful if the organization of data in your form does
1453 not map to an existing or database object in an automatic way, and you need
1454 to create a different type of object for initialization. (You might also
1455 want to do 'update_model' yourself.)
1456  
1457 Also see the 'use_init_obj_over_item' and the 'use_init_obj_when_no_accessor_in_item'
1458 flags, if you want to provide both an item and an init_object, and use the
1459 values from the init_object.
1460  
1461 The 'use_init_obj_when_no_accessor_in_item' flag is particularly useful
1462 when some of the fields in your form come from the database and some
1463 are process or environment type flags that are not in the database. You
1464 can provide defaults from both a database row and an 'init_object.
1465  
1466 =head3 ctx
1467  
1468 Place to store application context for your use in your form's methods.
1469  
1470 =head3 language_handle
1471  
1472 See 'language_handle' and '_build_language_handle' in
1473 L<HTML::FormHandler::TraitFor::I18N>.
1474  
1475 =head3 dependency
1476  
1477 Arrayref of arrayrefs of fields. If one of a group of fields has a
1478 value, then all of the group are set to 'required'.
1479  
1480 has '+dependency' => ( default => sub { [
1481 ['street', 'city', 'state', 'zip' ],] }
1482 );
1483  
1484 =head2 Flags
1485  
1486 =head3 validated, is_valid
1487  
1488 Flag that indicates if form has been validated. You might want to use
1489 this flag if you're doing something in between process and returning,
1490 such as setting a stash key. ('is_valid' is a synonym for this flag)
1491  
1492 $form->process( ... );
1493 $c->stash->{...} = ...;
1494 return unless $form->validated;
1495  
1496 =head3 ran_validation
1497  
1498 Flag to indicate that validation has been run. This flag will be
1499 false when the form is initially loaded and displayed, since
1500 validation is not run until FormHandler has params to validate.
1501  
1502 =head3 verbose, dump, peek
1503  
1504 Flag to dump diagnostic information. See 'dump_fields' and
1505 'dump_validated'. 'Peek' can be useful in diagnosing bugs.
1506 It will dump a brief listing of the fields and results.
1507  
1508 $form->process( ... );
1509 $form->peek;
1510  
1511 =head3 html_prefix
1512  
1513 Flag to indicate that the form name is used as a prefix for fields
1514 in an HTML form. Useful for multiple forms
1515 on the same HTML page. The prefix is stripped off of the fields
1516 before creating the internal field name, and added back in when
1517 returning a parameter hash from the 'fif' method. For example,
1518 the field name in the HTML form could be "book.borrower", and
1519 the field name in the FormHandler form (and the database column)
1520 would be just "borrower".
1521  
1522 has '+name' => ( default => 'book' );
1523 has '+html_prefix' => ( default => 1 );
1524  
1525 Also see the Field attribute "html_name", a convenience function which
1526 will return the form name + "." + field full_name
1527  
1528 =head3 is_html5
1529  
1530 Flag to indicate the fields will render using specialized attributes for html5.
1531 Set to 0 by default.
1532  
1533 =head3 use_defaults_over_obj
1534  
1535 The 'normal' precedence is that if there is an accessor in the item/init_object
1536 that value is used and not the 'default'. This flag makes the defaults of higher
1537 precedence. Mainly useful if providing an empty row on create.
1538  
1539 =head3 use_init_obj_over_item
1540  
1541 If you are providing both an item and an init_object, and want the init_object
1542 to be used for defaults instead of the item.
1543  
1544 =head2 For use in HTML
1545  
1546 form_element_attr - hashref for setting arbitrary HTML attributes
1547 set in form with: sub build_form_element_attr {...}
1548 form_element_class - arrayref for setting form tag class
1549 form_wrapper_attr - hashref for form wrapper element attributes
1550 set in form with: sub build_form_wrapper_attr {...}
1551 form_wrapper_class - arrayref for setting wrapper class
1552 do_form_wrapper - flag to wrap the form
1553 http_method - For storing 'post' or 'get'
1554 action - Store the form 'action' on submission. No default value.
1555 uuid - generates a string containing an HTML field with UUID
1556 form_tags - hashref of tags for use in rendering code
1557 widget_tags - rendering tags to be transferred to fields
1558  
1559 Discouraged (use form_element_attr instead):
1560  
1561 style - adds a 'style' attribute to the form tag
1562 enctype - Request enctype
1563  
1564 Note that the form tag contains an 'id' attribute which is set to the
1565 form name. The standards have been flip-flopping over whether a 'name'
1566 attribute is valid. It can be set with 'form_element_attr'.
1567  
1568 The rendering of the HTML attributes is done using the 'process_attrs'
1569 function and the 'element_attributes' or 'wrapper_attributes' method,
1570 which adds other attributes in for backward compatibility, and calls
1571 the 'html_attributes' hook.
1572  
1573 For HTML attributes, there is a form method hook, 'html_attributes',
1574 which can be used to customize/modify/localize form & field HTML attributes.
1575 Types: element, wrapper, label, form_element, form_wrapper, checkbox_label
1576  
1577 sub html_attributes {
1578 my ( $self, $obj, $type, $attrs, $result ) = @_;
1579  
1580 # obj is either form or field
1581 $attr->{class} = 'label' if $type eq 'label';
1582 $attr->{placeholder} = $self->_localize($attr->{placeholder})
1583 if exists $attr->{placeholder};
1584 return $attr;
1585 }
1586  
1587 Also see the documentation in L<HTML::FormHandler::Field> and in
1588 L<HTML::FormHandler::Manual::Rendering>.
1589  
1590 =head1 SUPPORT
1591  
1592 IRC:
1593  
1594 Join #formhandler on irc.perl.org
1595  
1596 Mailing list:
1597  
1598 http://groups.google.com/group/formhandler
1599  
1600 Code repository:
1601  
1602 http://github.com/gshank/html-formhandler/tree/master
1603  
1604 Bug tracker:
1605  
1606 https://rt.cpan.org/Dist/Display.html?Name=HTML-FormHandler
1607  
1608 =head1 SEE ALSO
1609  
1610 L<HTML::FormHandler::Manual>
1611  
1612 L<HTML::FormHandler::Manual::Tutorial>
1613  
1614 L<HTML::FormHandler::Manual::Intro>
1615  
1616 L<HTML::FormHandler::Manual::Templates>
1617  
1618 L<HTML::FormHandler::Manual::Cookbook>
1619  
1620 L<HTML::FormHandler::Manual::Rendering>
1621  
1622 L<HTML::FormHandler::Manual::Reference>
1623  
1624 L<HTML::FormHandler::Field>
1625  
1626 L<HTML::FormHandler::Model::DBIC>
1627  
1628 L<HTML::FormHandler::Render::Simple>
1629  
1630 L<HTML::FormHandler::Render::Table>
1631  
1632 L<HTML::FormHandler::Moose>
1633  
1634 =head1 CONTRIBUTORS
1635  
1636 gshank: Gerda Shank E<lt>gshank@cpan.orgE<gt>
1637  
1638 zby: Zbigniew Lukasiak E<lt>zby@cpan.orgE<gt>
1639  
1640 t0m: Tomas Doran E<lt>bobtfish@bobtfish.netE<gt>
1641  
1642 augensalat: Bernhard Graf E<lt>augensalat@gmail.comE<gt>
1643  
1644 cubuanic: Oleg Kostyuk E<lt>cub.uanic@gmail.comE<gt>
1645  
1646 rafl: Florian Ragwitz E<lt>rafl@debian.orgE<gt>
1647  
1648 mazpe: Lester Ariel Mesa
1649  
1650 dew: Dan Thomas
1651  
1652 koki: Klaus Ita
1653  
1654 jnapiorkowski: John Napiorkowski
1655  
1656 lestrrat: Daisuke Maki
1657  
1658 hobbs: Andrew Rodland
1659  
1660 Andy Clayton
1661  
1662 boghead: Bryan Beeley
1663  
1664 Csaba Hetenyi
1665  
1666 Eisuke Oishi
1667  
1668 Lian Wan Situ
1669  
1670 Murray
1671  
1672 Nick Logan
1673  
1674 Vladimir Timofeev
1675  
1676 diegok: Diego Kuperman
1677  
1678 ijw: Ian Wells
1679  
1680 amiri: Amiri Barksdale
1681  
1682 ozum: Ozum Eldogan
1683  
1684 lukast: Lukas Thiemeier
1685  
1686 Initially based on the source code of L<Form::Processor> by Bill Moseley
1687  
1688 =head1 AUTHOR
1689  
1690 FormHandler Contributors - see HTML::FormHandler
1691  
1692 =head1 COPYRIGHT AND LICENSE
1693  
1694 This software is copyright (c) 2017 by Gerda Shank.
1695  
1696 This is free software; you can redistribute it and/or modify it under
1697 the same terms as the Perl 5 programming language system itself.
1698  
1699 =cut
« FormHandler/Field/Date.pm ↑ Summary FormHandler/Render/Simple.pm »