| 1 | package Data::MuForm::Field::Compound; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: field consisting of subfields | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 33 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 33 | extends 'Data::MuForm::Field'; | |||||||||||||||||||||||||||
| 6 | 33 | with 'Data::MuForm::Fields'; | |||||||||||||||||||||||||||
| 7 | 33 | ▶ | use Data::MuForm::Meta; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 33 | ▶ | use Types::Standard ('Bool', 'ArrayRef'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 68 | ▶ | sub is_compound {1} | ||||||||||||||||||||||||||
|
Subroutine
is_compound: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 33 | has 'obj' => ( is => 'rw', clearer => 'clear_obj' ); | |||||||||||||||||||||||||||
| 13 | 33 | has 'primary_key' => ( is => 'rw', isa => ArrayRef, | |||||||||||||||||||||||||||
| 14 | predicate => 'has_primary_key', ); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has '+field_namespace' => ( | ||||||||||||||||||||||||||||
| 17 | default => sub { | ||||||||||||||||||||||||||||
| 18 | 96 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 19 | 96 | ▶ | return $self->form->field_namespace | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->form and $self->form->field_namespace
MC/DC0%0/2
$self->form and $self->form->field_namespace
$self->form
$self->form->field_namespace
Truth table33%1/3
A and B
A= $self->formB= $self->form->field_namespace
| |||||||||||||||||||||||||||||
| 20 | if $self->form && $self->form->field_namespace; | ||||||||||||||||||||||||||||
| 21 | 96 | return []; | |||||||||||||||||||||||||||
| 22 | }, | ||||||||||||||||||||||||||||
| 23 | 33 | ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub BUILD { | ||||||||||||||||||||||||||||
| 26 | 96 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 96 | $self->build_fields; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # this is for testing compound fields outside | ||||||||||||||||||||||||||||
| 31 | # of a form | ||||||||||||||||||||||||||||
| 32 | sub test_field_validate { | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
test_field_validate: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | unless( $self->form ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if( $self->has_input ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $self->fill_from_params( $self->input ); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||||||||||
| 39 | 0 | $self->fill_from_fields(); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | $self->field_validate; | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | unless( $self->form ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | foreach my $err_fld (@{$self->error_fields}) { | |||||||||||||||||||||||||||
| 45 | 0 | $self->push_error($err_fld->all_errors); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | around 'fill_from_object' => sub { | ||||||||||||||||||||||||||||
| 51 | 45 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 52 | 45 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | 45 | my ( $obj ) = @_; | |||||||||||||||||||||||||||
| 54 | 45 | ▶ | $self->obj($obj) if $obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 45 | $self->$orig(@_); | |||||||||||||||||||||||||||
| 56 | 33 | }; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | after 'clear_data' => sub { | ||||||||||||||||||||||||||||
| 59 | 62 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 62 | $self->clear_obj; | |||||||||||||||||||||||||||
| 61 | 33 | }; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | around 'fill_from_params' => sub { | ||||||||||||||||||||||||||||
| 64 | 62 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 65 | 62 | my $self = shift; | |||||||||||||||||||||||||||
| 66 | 62 | my ( $input, $exists ) = @_; | |||||||||||||||||||||||||||
| 67 | 62 | ▶ | if ( !$input && !$exists ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$input || $exists
MC/DC50%1/2
$input || $exists
$input
$exists
Truth table66%2/3
A || B
A= $inputB= $exists
| |||||||||||||||||||||||||||||
| 68 | 7 | return $self->fill_from_fields(); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | 55 | return $self->$orig(@_); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 33 | }; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub base_render_args { | ||||||||||||||||||||||||||||
| 76 | 11 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
base_render_args: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 11 | my $args = $self->next::method(@_); | |||||||||||||||||||||||||||
| 78 | 11 | $args->{is_compound} = 1; | |||||||||||||||||||||||||||
| 79 | 11 | return $args; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | sub render { | ||||||||||||||||||||||||||||
| 82 | 7 | ▶ | my ( $self, $rargs ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 7 | my $render_args = $self->get_render_args(%$rargs); | |||||||||||||||||||||||||||
| 84 | 7 | return $self->renderer->render_compound($render_args, $self->fields); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 33 | 1; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =pod | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 NAME | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Data::MuForm::Field::Compound - field consisting of subfields | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 VERSION | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | version 0.04 | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | This field class is designed as the base (parent) class for fields with | ||||||||||||||||||||||||||||
| 107 | multiple subfields. An example is L<Data::MuForm::Field::CompoundDateTime>. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | A compound parent class requires the use of sub-fields prepended | ||||||||||||||||||||||||||||
| 110 | with the parent class name plus a dot | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | has_field 'birthdate' => ( type => 'DateTime' ); | ||||||||||||||||||||||||||||
| 113 | has_field 'birthdate.year' => ( type => 'Year' ); | ||||||||||||||||||||||||||||
| 114 | has_field 'birthdate.month' => ( type => 'Month' ); | ||||||||||||||||||||||||||||
| 115 | has_field 'birthdate.day' => ( type => 'MonthDay'); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | If all validation is performed in the parent class so that no | ||||||||||||||||||||||||||||
| 118 | validation is necessary in the child classes, then the field class | ||||||||||||||||||||||||||||
| 119 | 'Nested' may be used. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | The array of subfields is available in the 'fields' array in | ||||||||||||||||||||||||||||
| 122 | the compound field: | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | $form->field('birthdate')->fields | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Error messages will be available in the field on which the error | ||||||||||||||||||||||||||||
| 127 | occurred. You can access 'error_fields' on the form or on Compound | ||||||||||||||||||||||||||||
| 128 | fields (and subclasses, like Repeatable). | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | The process method of this field runs the process methods on the child fields | ||||||||||||||||||||||||||||
| 131 | and then builds a hash of these fields values. This hash is available for | ||||||||||||||||||||||||||||
| 132 | further processing by L<Data::MuForm::Field/actions> and the validate method. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Gerda Shank | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | This software is copyright (c) 2017 by Gerda Shank. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 143 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||