Debian Codegraph @

lib/HTML/FormHandler.pm

raw
1 package HTML::FormHandler;
2 # ABSTRACT: HTML forms using Moose
3 $HTML::FormHandler::VERSION = '0.410002';
4 use Moose;
5 extends 'HTML::FormHandler::Base'; # to make some methods overridable by roles
6 with 'HTML::FormHandler::Model', 'HTML::FormHandler::Fields',
7 'HTML::FormHandler::BuildFields',
8 'HTML::FormHandler::TraitFor::I18N';
9 with 'HTML::FormHandler::InitResult';
10 with 'HTML::FormHandler::Widget::ApplyRole';
11 with 'HTML::FormHandler::Traits';
12 with 'HTML::FormHandler::Blocks';
13
14 use Carp;
15 use Class::MOP;
16 use HTML::FormHandler::Result;
17 use HTML::FormHandler::Field;
18 use Try::Tiny;
19 use MooseX::Types::LoadableClass qw/ LoadableClass /;
20 use namespace::autoclean;
21 use HTML::FormHandler::Merge ('merge');
22 use Sub::Name;
23 use Data::Clone;
24
25 use 5.008;
26
27
28 # for consistency in api with field nodes
29 sub form { shift }
30 sub is_form { 1 }
31 sub has_form { 1 }
32
33 # Moose attributes
34 has 'name' => (
35 isa => 'Str',
36 is => 'rw',
37 default => sub { return 'form' . int( rand 1000 ) }
38 );
39 sub full_name { '' }
40 sub full_accessor { '' }
41 has 'parent' => ( is => 'rw' );
42 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 my $self = shift;
62 my $result_class = 'HTML::FormHandler::Result';
63 if ( $self->widget_form ) {
64 my $role = $self->get_widget_role( $self->widget_form, 'Form' );
65 $result_class = $result_class->with_traits( $role );
66 }
67 my $result = $result_class->new( name => $self->name, form => $self );
68 return $result;
69 }
70
71 has 'index' => (
72 is => 'ro', isa => 'HashRef[HTML::FormHandler::Field]', traits => ['Hash'],
73 default => sub {{}},
74 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 traits => ['Array'], default => sub {[]},
82 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 default => sub {[]}, handles => { 'has_field_traits' => 'count' } );
91 has 'widget_name_space' => (
92 is => 'ro',
93 isa => 'HFH::ArrayRefStr',
94 traits => ['Array'],
95 default => sub {[]},
96 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 has 'widget_form' => ( is => 'ro', isa => 'Str', default => 'Simple', writer => 'set_widget_form' );
103 has 'widget_wrapper' => ( is => 'ro', isa => 'Str', default => 'Simple', writer => 'set_widget_wrapper' );
104 has 'do_form_wrapper' => ( is => 'rw', builder => 'build_do_form_wrapper' );
105 sub build_do_form_wrapper { 0 }
106 has 'no_widgets' => ( is => 'ro', isa => 'Bool' );
107 has 'no_preload' => ( is => 'ro', isa => 'Bool' );
108 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 default => sub {[]},
114 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 default => sub {[]},
125 handles => {
126 add_inactive => 'push',
127 has_inactive => 'count',
128 clear_inactive => 'clear',
129 }
130 );
131
132
133 # object with which to initialize
134 has 'init_object' => ( is => 'rw', clearer => 'clear_init_object' );
135 has 'update_field_list' => ( is => 'rw',
136 isa => 'HashRef',
137 default => sub {{}},
138 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 has 'defaults' => ( is => 'rw', isa => 'HashRef', default => sub {{}}, traits => ['Hash'],
146 handles => { has_defaults => 'count', clear_defaults => 'clear' },
147 );
148 has 'use_defaults_over_obj' => ( is => 'rw', isa => 'Bool', clearer => 'clear_use_defaults_over_obj' );
149 has 'use_init_obj_over_item' => ( is => 'rw', isa => 'Bool', clearer => 'clear_use_init_obj_over_item' );
150 has 'use_init_obj_when_no_accessor_in_item' => ( is => 'rw', isa => 'Bool' );
151 has 'use_fields_for_input_without_param' => ( is => 'rw', isa => 'Bool' );
152 # flags
153 has [ 'verbose', 'processed', 'did_init_obj' ] => ( isa => 'Bool', is => 'rw' );
154 has 'user_data' => ( isa => 'HashRef', is => 'rw' );
155 has 'ctx' => ( is => 'rw', weak_ref => 1, clearer => 'clear_ctx' );
156 has 'html_prefix' => ( isa => 'Bool', is => 'ro' );
157 has 'active_column' => ( isa => 'Str', is => 'ro' );
158 has 'http_method' => ( isa => 'Str', is => 'ro', default => 'post' );
159 has 'enctype' => ( is => 'rw', isa => 'Str' );
160 has 'error_message' => ( is => 'rw', predicate => 'has_error_message', clearer => 'clear_error_message' );
161 has 'success_message' => ( is => 'rw', predicate => 'has_success_message', clearer => 'clear_success_message' );
162 has 'info_message' => ( is => 'rw', predicate => 'has_info_message', clearer => 'clear_info_message' );
163 # deprecated
164 has 'style' => ( isa => 'Str', is => 'rw' );
165
166 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 default => sub { {} }, handles => { has_html_attr => 'count',
170 set_html_attr => 'set', delete_html_attr => 'delete' },
171 trigger => \&_html_attr_set,
172 );
173 sub _html_attr_set {
174 my ( $self, $value ) = @_;
175 my $class = delete $value->{class};
176 $self->form_element_attr($value);
177 $self->add_form_element_class if $class;
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 no strict 'refs';
185 foreach my $attr ('form_wrapper', 'form_element' ) {
186 my $add_meth = "add_${attr}_class";
187 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 my $attr_builder = __PACKAGE__ . "::build_${attr}_attr";
199 *$attr_builder = subname $attr_builder, sub {{}};
200 # create the 'class' slots
201 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 my $class_builder = __PACKAGE__ . "::build_${attr}_class";
212 *$class_builder = subname $class_builder, sub {[]};
213 # create wrapper for add_to_ to accept arrayref
214 my $add_to_class = __PACKAGE__ . "::add_${attr}_class";
215 my $_add_meth = __PACKAGE__ . "::_add_${attr}_class";
216 # create add method that takes an arrayref
217 *$add_to_class = subname $add_to_class, sub { shift->$_add_meth((ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_)); }
218 }
219 }
220
221 sub attributes { shift->form_element_attributes(@_) }
222 sub form_element_attributes {
223 my ( $self, $result ) = @_;
224 $result ||= $self->result;
225 my $attr = {};
226 $attr->{id} = $self->name;
227 $attr->{action} = $self->action if $self->action;
228 $attr->{method} = $self->http_method if $self->http_method;
229 $attr->{enctype} = $self->enctype if $self->enctype;
230 $attr->{style} = $self->style if $self->style;
231 $attr = {%$attr, %{$self->form_element_attr}};
232 my $class = [@{$self->form_element_class}];
233 $attr->{class} = $class if @$class;
234 my $mod_attr = $self->html_attributes($self, 'form_element', $attr);
235 return ref $mod_attr eq 'HASH' ? $mod_attr : $attr;
236 }
237 sub form_wrapper_attributes {
238 my ( $self, $result ) = @_;
239 $result ||= $self->result;
240 my $attr = {%{$self->form_wrapper_attr}};
241 my $class = [@{$self->form_wrapper_class}];
242 $attr->{class} = $class if @$class;
243 my $mod_attr = $self->html_attributes($self, 'form_wrapper', $attr);
244 return ref $mod_attr eq 'HASH' ? $mod_attr : $attr;
245 }
246
247 sub html_attributes {
248 my ( $self, $obj, $type, $attrs, $result ) = @_;
249 # deprecated 'field_html_attributes'; name changed, remove eventually
250 if( $self->can('field_html_attributes') ) {
251 $attrs = $self->field_html_attributes( $obj, $type, $attrs, $result );
252 }
253 return $attrs;
254 }
255
256 sub has_flag {
257 my ( $self, $flag_name ) = @_;
258 return unless $self->can($flag_name);
259 return $self->$flag_name;
260 }
261
262 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 sub build_form_tags {{}}
275 sub get_tag {
276 my ( $self, $name ) = @_;
277 return '' unless $self->tag_exists($name);
278 my $tag = $self->_get_tag($name);
279 return $self->$tag if ref $tag eq 'CODE';
280 return $tag unless $tag =~ /^%/;
281 ( my $block_name = $tag ) =~ s/^%//;
282 return $self->form->block($block_name)->render
283 if ( $self->form && $self->form->block_exists($block_name) );
284 return '';
285 }
286 has 'for_js' => (
287 isa => 'HashRef',
288 traits => ['Hash'],
289 is => 'rw',
290 default => sub { {} },
291 handles => {
292 set_for_js => 'set',
293 has_for_js => 'count',
294 clear_for_js => 'clear',
295 }
296 );
297
298 has 'action' => ( is => 'rw' );
299 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 default => sub { {} },
305 trigger => sub { shift->_munge_params(@_) },
306 handles => {
307 set_param => 'set',
308 get_param => 'get',
309 clear_params => 'clear',
310 has_params => 'count',
311 },
312 );
313 sub submitted { shift->has_params }
314 has 'dependency' => ( isa => 'ArrayRef', is => 'rw' );
315 has '_required' => (
316 traits => ['Array'],
317 isa => 'ArrayRef[HTML::FormHandler::Field]',
318 is => 'rw',
319 default => sub { [] },
320 handles => {
321 clear_required => 'clear',
322 add_required => 'push',
323 }
324 );
325
326 # these messages could apply to either fields or form
327 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 sub build_messages { {} }
338
339 my $class_messages = {};
340 sub get_class_messages {
341 return $class_messages;
342 }
343
344 sub get_message {
345 my ( $self, $msg ) = @_;
346 return $self->_get_form_message($msg) if $self->_has_form_message($msg);
347 return $self->get_class_messages->{$msg};
348 }
349 sub all_messages {
350 my $self = shift;
351 return { %{$self->get_class_messages}, %{$self->messages} };
352 }
353
354 has 'params_class' => (
355 is => 'ro',
356 isa => LoadableClass,
357 coerce => 1,
358 default => 'HTML::FormHandler::Params',
359 );
360
361 has 'params_args' => ( is => 'ro', isa => 'ArrayRef' );
362
363 sub BUILDARGS {
364 my $class = shift;
365
366 if ( scalar @_ == 1 && ref( $_[0]) ne 'HASH' ) {
367 my $arg = $_[0];
368 return blessed($arg) ? { item => $arg } : { item_id => $arg };
369 }
370 return $class->SUPER::BUILDARGS(@_);
371 }
372
373 sub BUILD {
374 my $self = shift;
375
376 $self->before_build; # hook to allow customizing forms
377 # HTML::FormHandler::Widget::Form::Simple is applied in Base
378 $self->apply_widget_role( $self, $self->widget_form, 'Form' )
379 unless ( $self->no_widgets || $self->widget_form eq 'Simple' );
380 $self->_build_fields; # create the form fields (BuildFields.pm)
381 $self->build_active if $self->has_active || $self->has_inactive || $self->has_flag('is_wizard');
382 $self->after_build; # hook for customizing
383 return if defined $self->item_id && !$self->item;
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 unless( $self->no_preload ) {
390 if ( my $init_object = $self->use_init_obj_over_item ?
391 ($self->init_object || $self->item) : ( $self->item || $self->init_object ) ) {
392 $self->_result_from_object( $self->result, $init_object );
393 }
394 else {
395 $self->_result_from_fields( $self->result );
396 }
397 }
398 $self->dump_fields if $self->verbose;
399 return;
400 }
401 sub before_build {}
402 sub after_build {}
403
404 sub process {
405 my $self = shift;
406
407 warn "HFH: process ", $self->name, "\n" if $self->verbose;
408 $self->clear if $self->processed;
409 $self->setup_form(@_);
410 $self->validate_form if $self->posted;
411 $self->update_model if ( $self->validated && !$self->no_update );
412 $self->after_update_model if ( $self->validated && !$self->no_update );
413 $self->dump_fields if $self->verbose;
414 $self->processed(1);
415 return $self->validated;
416 }
417
418 sub run {
419 my $self = shift;
420 $self->setup_form(@_);
421 $self->validate_form if $self->posted;
422 $self->update_model if ( $self->validated && !$self->no_update );;
423 my $result = $self->result;
424 $self->clear;
425 return $result;
426 }
427
428 sub after_update_model {
429 my $self = shift;
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 if ( $self->has_repeatable_fields && $self->item ) {
437 foreach my $field ( $self->all_repeatable_fields ) {
438 next unless $field->is_active;
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 my $needs_reload = 0;
443 foreach my $sub_field ( $field->fields ) {
444 if ( $sub_field->has_flag('is_compound') && $sub_field->has_primary_key ) {
445 foreach my $pk_field ( @{ $sub_field->primary_key } ) {
446 $needs_reload++ unless $pk_field->fif;
447 }
448 last if $needs_reload;
449 }
450 }
451 next unless $needs_reload;
452 my @names = split( /\./, $field->full_name );
453 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 if ( ref $rep_item ) {
458 my $parent = $field->parent;
459 my $result = $field->result;
460 $field->init_state;
461 my $new_result = $field->_result_from_object( $result, $rep_item );
462 # find index of existing result
463 my $index = $parent->result->find_result_index( sub { $_ == $result } );
464 # replace existing result with new result
465 $parent->result->set_result_at_index( $index, $new_result );
466 }
467 }
468 }
469 }
470
471
472 sub db_validate {
473 my $self = shift;
474 my $fif = $self->fif;
475 $self->process($fif);
476 return $self->validated;
477 }
478
479 sub clear {
480 my $self = shift;
481 $self->clear_data;
482 $self->clear_params;
483 $self->clear_posted;
484 $self->clear_item;
485 $self->clear_init_object;
486 $self->clear_ctx;
487 $self->processed(0);
488 $self->did_init_obj(0);
489 $self->clear_result;
490 $self->clear_use_defaults_over_obj;
491 $self->clear_use_init_obj_over_item;
492 $self->clear_no_update;
493 $self->clear_info_message;
494 $self->clear_for_js;
495 }
496
497 sub values { shift->value }
498
499 # deprecated?
500 sub error_field_names {
501 my $self = shift;
502 my @error_fields = $self->error_fields;
503 return map { $_->name } @error_fields;
504 }
505
506 sub errors {
507 my $self = shift;
508 my @error_fields = $self->error_fields;
509 my @errors = $self->all_form_errors;
510 push @errors, map { $_->all_errors } @error_fields;
511 return @errors;
512 }
513
514 sub errors_by_id {
515 my $self = shift;
516 my %errors;
517 $errors{$_->id} = [$_->all_errors] for $self->error_fields;
518 return \%errors;
519 }
520
521 sub errors_by_name {
522 my $self = shift;
523 my %errors;
524 $errors{$_->html_name} = [$_->all_errors] for $self->error_fields;
525 return \%errors;
526 }
527
528 sub build_errors {
529 my $self = shift;
530 # this puts the errors in the result
531 foreach my $err_res (@{$self->result->error_results}) {
532 $self->result->_push_errors($err_res->all_errors);
533 }
534 }
535
536 sub uuid {
537 my $form = shift;
538 require Data::UUID;
539 my $uuid = Data::UUID->new->create_str;
540 return qq[<input type="hidden" name="form_uuid" value="$uuid">];
541 }
542
543 sub validate_form {
544 my $self = shift;
545 my $params = $self->params;
546 $self->_set_dependency; # set required dependencies
547 $self->_fields_validate;
548 $self->validate; # empty method for users
549 $self->validate_model; # model specific validation
550 $self->fields_set_value;
551 $self->build_errors; # move errors to result
552 $self->_clear_dependency;
553 $self->clear_posted;
554 $self->ran_validation(1);
555 $self->dump_validated if $self->verbose;
556 return $self->validated;
557 }
558
559 sub validate { 1 }
560
561 sub has_errors {
562 my $self = shift;
563 return $self->has_error_fields || $self->has_form_errors;
564 }
565 sub num_errors {
566 my $self = shift;
567 return $self->num_error_fields + $self->num_form_errors;
568 }
569 sub get_errors { shift->errors }
570
571 sub setup_form {
572 my ( $self, @args ) = @_;
573 if ( @args == 1 ) {
574 $self->params( $args[0] );
575 }
576 elsif ( @args > 1 ) {
577 my $hashref = {@args};
578 while ( my ( $key, $value ) = each %{$hashref} ) {
579 confess "invalid attribute '$key' passed to setup_form"
580 unless $self->can($key);
581 $self->$key($value);
582 }
583 }
584 if ( $self->item_id && !$self->item ) {
585 $self->item( $self->build_item );
586 }
587 $self->clear_result;
588 $self->set_active;
589 $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 $self->posted(1) if ( $self->has_params && !$self->has_posted );
595 if ( !$self->did_init_obj ) {
596 if ( my $init_object = $self->use_init_obj_over_item ?
597 ($self->init_object || $self->item) : ( $self->item || $self->init_object ) ) {
598 $self->_result_from_object( $self->result, $init_object );
599 }
600 elsif ( !$self->posted ) {
601 # no initial object. empty form must be initialized
602 $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 my $params = clone( $self->params );
607 if ( $self->posted ) {
608 $self->clear_result;
609 $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 my $self = shift;
617 if( $self->has_active ) {
618 foreach my $fname (@{$self->active}) {
619 my $field = $self->field($fname);
620 if ( $field ) {
621 $field->_active(1);
622 }
623 else {
624 warn "field $fname not found to set active";
625 }
626 }
627 $self->clear_active;
628 }
629 if( $self->has_inactive ) {
630 foreach my $fname (@{$self->inactive}) {
631 my $field = $self->field($fname);
632 if ( $field ) {
633 $field->_active(0);
634 }
635 else {
636 warn "field $fname not found to set inactive";
637 }
638 }
639 $self->clear_inactive;
640 }
641 }
642
643 # if active => [...] is set at build time, remove 'inactive' flags
644 sub build_active {
645 my $self = shift;
646 if( $self->has_active ) {
647 foreach my $fname (@{$self->active}) {
648 my $field = $self->field($fname);
649 if( $field ) {
650 $field->clear_inactive;
651 }
652 else {
653 warn "field $fname not found to set active";
654 }
655 }
656 $self->clear_active;
657 }
658 if( $self->has_inactive ) {
659 foreach my $fname (@{$self->inactive}) {
660 my $field = $self->field($fname);
661 if( $field ) {
662 $field->inactive(1);
663 }
664 else {
665 warn "field $fname not found to set inactive";
666 }
667 }
668 $self->clear_inactive;
669 }
670 }
671
672 sub fif { shift->fields_fif(@_) }
673
674 # this is subclassed by the model, which may
675 # do a lot more than this
676 sub init_value {
677 my ( $self, $field, $value ) = @_;
678 $field->init_value($value);
679 $field->_set_value($value);
680 }
681
682 sub _set_dependency {
683 my $self = shift;
684
685 my $depends = $self->dependency || return;
686 my $params = $self->params;
687 for my $group (@$depends) {
688 next if @$group < 2;
689 # process a group of fields
690 for my $name (@$group) {
691 # is there a value?
692 my $value = $params->{$name};
693 next unless defined $value;
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 my $field = $self->field($name);
697 next if $self->field($name)->type eq 'Boolean' && $value == 0;
698 next unless HTML::FormHandler::Field::has_some_value($value);
699 # one field was found non-blank, so set all to required
700 for (@$group) {
701 my $field = $self->field($_);
702 next unless $field && !$field->required;
703 $self->add_required($field); # save for clearing later.
704 $field->required(1);
705 }
706 last;
707 }
708 }
709 }
710
711 sub _clear_dependency {
712 my $self = shift;
713
714 $_->required(0) for @{$self->_required};
715 $self->clear_required;
716 }
717
718 sub peek {
719 my $self = shift;
720 my $string = "Form " . $self->name . "\n";
721 my $indent = ' ';
722 foreach my $field ( $self->sorted_fields ) {
723 $string .= $field->peek( $indent );
724 }
725 return $string;
726 }
727
728 sub _munge_params {
729 my ( $self, $params, $attr ) = @_;
730 my $_fix_params = $self->params_class->new( @{ $self->params_args || [] } );
731 my $new_params = $_fix_params->expand_hash($params);
732 if ( $self->html_prefix ) {
733 $new_params = $new_params->{ $self->name };
734 }
735 $new_params = {} if !defined $new_params;
736 $self->{params} = $new_params;
737 }
738
739 sub params_to_values {
740 my ( $self, $params ) = @_;
741 my $_fix_params = $self->params_class->new( @{ $self->params_args || [] } );
742 my $new_params = $_fix_params->expand_hash($params);
743 return $new_params;
744 }
745
746 sub add_form_error {
747 my ( $self, @message ) = @_;
748
749 unless ( defined $message[0] ) {
750 @message = ('form is invalid');
751 }
752 my $out;
753 try {
754 $out = $self->_localize(@message);
755 }
756 catch {
757 die "Error occurred localizing error message for " . $self->name . ". $_";
758 };
759 $self->push_form_errors($out);
760 return;
761 }
762
763 sub get_default_value { }
764 sub _can_deflate { }
765
766 sub update_fields {
767 my $self = shift;
768 if( $self->has_update_field_list ) {
769 my $updates = $self->update_field_list;
770 foreach my $field_name ( keys %{$updates} ) {
771 $self->update_field($field_name, $updates->{$field_name} );
772 }
773 $self->clear_update_field_list;
774 }
775 if( $self->has_defaults ) {
776 my $defaults = $self->defaults;
777 foreach my $field_name ( keys %{$defaults} ) {
778 $self->update_field($field_name, { default => $defaults->{$field_name} } );
779 }
780 $self->clear_defaults;
781 }
782 }
783
784 sub update_field {
785 my ( $self, $field_name, $updates ) = @_;
786
787 my $field = $self->field($field_name);
788 unless( $field ) {
789 die "Field $field_name is not found and cannot be updated by update_fields";
790 }
791 while ( my ( $attr_name, $attr_value ) = each %{$updates} ) {
792 confess "invalid attribute '$attr_name' passed to update_field"
793 unless $field->can($attr_name);
794 if( $attr_name eq 'tags' ) {
795 $field->set_tag(%$attr_value);
796 }
797 else {
798 $field->$attr_name($attr_value);
799 }
800 }
801 }
802
803
804 __PACKAGE__->meta->make_immutable;
805 use namespace::autoclean;
806 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.410002
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>[email protected]<gt>
1637
1638 zby: Zbigniew Lukasiak E<lt>[email protected]<gt>
1639
1640 t0m: Tomas Doran E<lt>[email protected]<gt>
1641
1642 augensalat: Bernhard Graf E<lt>[email protected]<gt>
1643
1644 cubuanic: Oleg Kostyuk E<lt>[email protected]<gt>
1645
1646 rafl: Florian Ragwitz E<lt>[email protected]<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