| 1 |
package HTML::FormHandler::Field::Compound; |
| 2 |
# ABSTRACT: field consisting of subfields |
| 3 |
$HTML::FormHandler::Field::Compound::VERSION = '0.410002'; |
| 4 |
use Moose; |
| 5 |
extends 'HTML::FormHandler::Field'; |
| 6 |
with 'HTML::FormHandler::Fields'; |
| 7 |
with 'HTML::FormHandler::BuildFields'; |
| 8 |
with 'HTML::FormHandler::InitResult'; |
| 9 |
|
| 10 |
|
| 11 |
has '+widget' => ( default => 'Compound' ); |
| 12 |
has 'is_compound' => ( is => 'ro', isa => 'Bool', default => 1 ); |
| 13 |
has 'item' => ( is => 'rw', clearer => 'clear_item' ); |
| 14 |
has '+do_wrapper' => ( default => 0 ); |
| 15 |
has '+do_label' => ( default => 0 ); |
| 16 |
has 'primary_key' => ( is => 'rw', isa => 'ArrayRef', |
| 17 |
predicate => 'has_primary_key', ); |
| 18 |
|
| 19 |
has '+field_name_space' => ( |
| 20 |
default => sub { |
| 21 |
my $self = shift; |
| 22 |
return $self->form->field_name_space |
| 23 |
if $self->form && $self->form->field_name_space; |
| 24 |
return []; |
| 25 |
}, |
| 26 |
); |
| 27 |
|
| 28 |
sub BUILD { |
| 29 |
my $self = shift; |
| 30 |
$self->_build_fields; |
| 31 |
} |
| 32 |
|
| 33 |
# this is for testing compound fields outside |
| 34 |
# of a form |
| 35 |
sub test_validate_field { |
| 36 |
my $self = shift; |
| 37 |
unless( $self->form ) { |
| 38 |
if( $self->has_input ) { |
| 39 |
$self->_result_from_input( $self->result, $self->input );; |
| 40 |
} |
| 41 |
else { |
| 42 |
$self->_result_from_fields( $self->result ); |
| 43 |
} |
| 44 |
} |
| 45 |
$self->validate_field; |
| 46 |
unless( $self->form ) { |
| 47 |
foreach my $err_res (@{$self->result->error_results}) { |
| 48 |
$self->result->_push_errors($err_res->all_errors); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
around '_result_from_object' => sub { |
| 54 |
my $orig = shift; |
| 55 |
my $self = shift; |
| 56 |
my ( $self_result, $item ) = @_; |
| 57 |
$self->item($item) if $item; |
| 58 |
$self->$orig(@_); |
| 59 |
}; |
| 60 |
|
| 61 |
after 'clear_data' => sub { |
| 62 |
my $self = shift; |
| 63 |
$self->clear_item; |
| 64 |
}; |
| 65 |
|
| 66 |
around '_result_from_input' => sub { |
| 67 |
my $orig = shift; |
| 68 |
my $self = shift; |
| 69 |
my ( $self_result, $input, $exists ) = @_; |
| 70 |
if ( !$input && !$exists ) { |
| 71 |
return $self->_result_from_fields($self_result); |
| 72 |
} |
| 73 |
else { |
| 74 |
return $self->$orig(@_); |
| 75 |
} |
| 76 |
}; |
| 77 |
|
| 78 |
__PACKAGE__->meta->make_immutable; |
| 79 |
use namespace::autoclean; |
| 80 |
1; |
| 81 |
|
| 82 |
__END__ |
| 83 |
|
| 84 |
=pod |
| 85 |
|
| 86 |
=encoding UTF-8 |
| 87 |
|
| 88 |
=head1 NAME |
| 89 |
|
| 90 |
HTML::FormHandler::Field::Compound - field consisting of subfields |
| 91 |
|
| 92 |
=head1 VERSION |
| 93 |
|
| 94 |
version 0.410002 |
| 95 |
|
| 96 |
=head1 SYNOPSIS |
| 97 |
|
| 98 |
This field class is designed as the base (parent) class for fields with |
| 99 |
multiple subfields. Examples are L<HTML::FormHandler::Field::DateTime> |
| 100 |
and L<HTML::FormHandler::Field::Duration>. |
| 101 |
|
| 102 |
A compound parent class requires the use of sub-fields prepended |
| 103 |
with the parent class name plus a dot |
| 104 |
|
| 105 |
has_field 'birthdate' => ( type => 'DateTime' ); |
| 106 |
has_field 'birthdate.year' => ( type => 'Year' ); |
| 107 |
has_field 'birthdate.month' => ( type => 'Month' ); |
| 108 |
has_field 'birthdate.day' => ( type => 'MonthDay'); |
| 109 |
|
| 110 |
If all validation is performed in the parent class so that no |
| 111 |
validation is necessary in the child classes, then the field class |
| 112 |
'Nested' may be used. |
| 113 |
|
| 114 |
The array of subfields is available in the 'fields' array in |
| 115 |
the compound field: |
| 116 |
|
| 117 |
$form->field('birthdate')->fields |
| 118 |
|
| 119 |
Error messages will be available in the field on which the error |
| 120 |
occurred. You can access 'error_fields' on the form or on Compound |
| 121 |
fields (and subclasses, like Repeatable). |
| 122 |
|
| 123 |
The process method of this field runs the process methods on the child fields |
| 124 |
and then builds a hash of these fields values. This hash is available for |
| 125 |
further processing by L<HTML::FormHandler::Field/actions> and the validate method. |
| 126 |
|
| 127 |
=head2 widget |
| 128 |
|
| 129 |
Widget type is 'compound' |
| 130 |
|
| 131 |
=head2 build_update_subfields |
| 132 |
|
| 133 |
You can set 'defaults' or other settings in a 'build_update_subfields' method, |
| 134 |
which contains attribute settings that will be merged with field definitions |
| 135 |
when the fields are built. Use the 'by_flag' key with 'repeatable', 'compound', |
| 136 |
and 'contains' subkeys, or use the 'all' key for settings which apply to all |
| 137 |
subfields in the compound field. |
| 138 |
|
| 139 |
=head1 AUTHOR |
| 140 |
|
| 141 |
FormHandler Contributors - see HTML::FormHandler |
| 142 |
|
| 143 |
=head1 COPYRIGHT AND LICENSE |
| 144 |
|
| 145 |
This software is copyright (c) 2017 by Gerda Shank. |
| 146 |
|
| 147 |
This is free software; you can redistribute it and/or modify it under |
| 148 |
the same terms as the Perl 5 programming language system itself. |
| 149 |
|
| 150 |
=cut |