File Coverage

blib/lib/Data/MuForm/Field/Compound.pm
Criterion Covered Total %
statement 19 29 65.5
branch 0 6 0.0
condition n/a
subroutine 7 8 87.5
pod 0 5 0.0
total 26 48 54.1


line stmt bran cond sub pod time code
1             package Data::MuForm::Field::Compound;
2             # ABSTRACT: field consisting of subfields
3              
4 33     33   357956 use Moo;
  33         46  
  33         163  
5             extends 'Data::MuForm::Field';
6             with 'Data::MuForm::Fields';
7 33     33   6701 use Data::MuForm::Meta;
  33         52  
  33         181  
8 33     33   10408 use Types::Standard ('Bool', 'ArrayRef');
  33         325720  
  33         293  
9              
10              
11 68     68 0 466 sub is_compound {1}
12             has 'obj' => ( is => 'rw', clearer => 'clear_obj' );
13             has 'primary_key' => ( is => 'rw', isa => ArrayRef,
14             predicate => 'has_primary_key', );
15              
16             has '+field_namespace' => (
17             default => sub {
18             my $self = shift;
19             return $self->form->field_namespace
20             if $self->form && $self->form->field_namespace;
21             return [];
22             },
23             );
24              
25             sub BUILD {
26 96     96 0 2134 my $self = shift;
27 96         1484 $self->build_fields;
28             }
29              
30             # this is for testing compound fields outside
31             # of a form
32             sub test_field_validate {
33 0     0 0 0 my $self = shift;
34 0 0       0 unless( $self->form ) {
35 0 0       0 if( $self->has_input ) {
36 0         0 $self->fill_from_params( $self->input );
37             }
38             else {
39 0         0 $self->fill_from_fields();
40             }
41             }
42 0         0 $self->field_validate;
43 0 0       0 unless( $self->form ) {
44 0         0 foreach my $err_fld (@{$self->error_fields}) {
  0         0  
45 0         0 $self->push_error($err_fld->all_errors);
46             }
47             }
48             }
49              
50             around 'fill_from_object' => sub {
51             my $orig = shift;
52             my $self = shift;
53             my ( $obj ) = @_;
54             $self->obj($obj) if $obj;
55             $self->$orig(@_);
56             };
57              
58             after 'clear_data' => sub {
59             my $self = shift;
60             $self->clear_obj;
61             };
62              
63             around 'fill_from_params' => sub {
64             my $orig = shift;
65             my $self = shift;
66             my ( $input, $exists ) = @_;
67             if ( !$input && !$exists ) {
68             return $self->fill_from_fields();
69             }
70             else {
71             return $self->$orig(@_);
72             }
73             };
74              
75             sub base_render_args {
76 11     11 0 810 my $self = shift;
77 11         47 my $args = $self->next::method(@_);
78 11         161 $args->{is_compound} = 1;
79 11         35 return $args;
80             }
81             sub render {
82 7     7 0 16 my ( $self, $rargs ) = @_;
83 7         45 my $render_args = $self->get_render_args(%$rargs);
84 7         136 return $self->renderer->render_compound($render_args, $self->fields);
85             }
86              
87              
88             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.03
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