MuForm/Field.pm

filter: Statementstmt 89.8% 397 / 442 Branchbran 78.5% 165 / 210 Conditioncond 63.4% 40 / 63 MC/DCmcdc 48.6% 18 / 37 Subroutinesub 90.0% 81 / 90 Podpod 15.7% 11 / 70 totaltotal 78.0% 712 / 912 SCAR 53.3
CC
135
Cov
84%
full_accessor
42.0
apply_actions
33.5
field_validate
25.3
CRAP 206.9
« MuForm/Localizer.pm ↑ Summary MuForm/Fields.pm »
1 package Data::MuForm::Field;
2 # ABSTRACT: Base field package
3 93 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 93 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 93 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 93 ▶use Scalar::Util ('blessed', 'weaken');
Subroutine BEGIN: called CC=1 SCAR=0.0
7 93 ▶use Data::Clone ('data_clone');
Subroutine BEGIN: called CC=1 SCAR=0.0
8 93 ▶use Data::MuForm::Localizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 93 ▶use Data::MuForm::Merge ('merge');
Subroutine BEGIN: called CC=1 SCAR=0.0
10 93 with 'Data::MuForm::Common';
11  
12 93 has 'name' => ( is => 'rw', required => 1 );
13 93 has 'id' => ( is => 'rw', lazy => 1, builder => 'build_id' );
14 sub build_id {
15 82 ▶ my $self = shift;
Subroutine build_id: called CC=4 SCAR=15.2
Pod documented
16 82 ▶ if ( my $meth = $self->get_method('build_id') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $meth = $self->get_method('build_id')) { } 3 79 82
elsif ($self->form and $self->form->can('build_field_id')) { } 0 79 79
17 3 return $meth->($self, @_);
18 }
19 ▶ elsif ( $self->form && $self->form->can('build_field_id') ) {
Condition33%1/3
$self->form and $self->form->can("build_field_id")
!ll&&!rl&&r
0790
MC/DC0%0/2
$self->form and $self->form->can("build_field_id")
$self->form $self->form->can("build_field_id")
Truth table33%1/3
A and B
A= $self->formB= $self->form->can("build_field_id")
ABresult
0X0
100
111
20 0 return $self->form->build_field_id($self);
21 }
22 79 return $self->prefixed_name;
23 }
24 93 has 'prefixed_name' => ( is => 'rw', lazy => 1, builder => 'build_prefixed_name');
25 sub build_prefixed_name {
26 80 ▶ my $self = shift;
Subroutine build_prefixed_name: called CC=3 SCAR=12.5
Pod undocumented
27 80 ▶ my $prefix = ( $self->form && $self->form->field_prefix ) ? $self->field_prefix. "." : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->form && $self->form->field_prefix ? : 0 80 80
Condition33%1/3
$self->form and $self->form->field_prefix
!ll&&!rl&&r
0800
MC/DC0%0/2
$self->form and $self->form->field_prefix
$self->form $self->form->field_prefix
Truth table33%1/3
A and B
A= $self->formB= $self->form->field_prefix
ABresult
0X0
100
111
28 80 return $prefix . $self->full_name;
29 }
30 93 has 'form' => ( is => 'rw', weak_ref => 1, predicate => 'has_form' );
31 93 has 'type' => ( is => 'ro', required => 1, default => 'Text' );
32 93 has 'default' => ( is => 'rw' );
33 93 has 'input' => ( is => 'rw', predicate => 'has_input', clearer => 'clear_input' );
34 93 has 'input_without_param' => ( is => 'rw', predicate => 'has_input_without_param' );
35 93 has 'value' => ( is => 'rw', predicate => 'has_value', clearer => 'clear_value' );
36 93 has 'init_value' => ( is => 'rw', predicate => 'has_init_value', clearer => 'clear_init_value' );
37 93 has 'no_value_if_empty' => ( is => 'rw' );
38 93 has 'input_param' => ( is => 'rw' );
39 93 has 'filled_from' => ( is => 'rw', clearer => 'clear_filled_from' );
40 93 has 'password' => ( is => 'rw', default => 0 );
41 93 has 'accessor' => ( is => 'rw', lazy => 1, builder => 'build_accessor' );
42 sub build_accessor {
43 443 ▶ my $self = shift;
Subroutine build_accessor: called CC=2 SCAR=7.0
Pod undocumented
44 443 my $accessor = $self->name;
45 443 ▶ $accessor =~ s/^(.*)\.//g if ( $accessor =~ /\./ );
Branch: 1 branch
BranchTrueFalseTotal
if $accessor =~ /\./ 0 443 443
46 443 return $accessor;
47 }
48 93 has 'custom' => ( is => 'rw' );
49 93 has 'parent' => ( is => 'rw', predicate => 'has_parent', weak_ref => 1 );
50 93 has 'source' => ( is => 'rw' );
51 641 ▶has 'errors' => ( is => 'rw', isa => ArrayRef, default => sub {[]} );
Subroutine __ANON__: called CC=1 SCAR=0.0
52 663 ▶sub has_errors { my $self = shift; return scalar @{$self->errors}; }
Subroutine has_errors: called CC=1 SCAR=0.0
Pod undocumented
53 18 ▶sub all_errors { my $self = shift; return @{$self->errors}; }
Subroutine all_errors: called CC=1 SCAR=0.0
Pod undocumented
54 750 ▶sub clear_errors { $_[0]->{errors} = [] }
Subroutine clear_errors: called CC=1 SCAR=0.0
Pod undocumented
55 ▶sub clear_error_fields { }
Subroutine clear_error_fields: called CC=1 SCAR=0.0
Pod undocumented
56  
57 # this is a permanent setting of active
58 93 has 'active' => ( is => 'rw', default => 1 );
59 # this is a temporary active set on the process call, cleared on clear_data
60 93 has '_active' => ( is => 'rw', predicate => '_has_active', clearer => '_clear_active' );
61 1 ▶sub clear_inactive { $_[0]->active(1) }
Subroutine clear_inactive: called CC=1 SCAR=0.0
Pod undocumented
62 0 ▶sub inactive { return ( shift->active ? 0 : 1 ) }
Subroutine inactive: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->active ? : 0 0 0
63 sub is_active {
64 4969 ▶ my $self = shift;
Subroutine is_active: called CC=2 SCAR=6.9
Pod documented
65 4969 ▶ return $self->_active if $self->_has_active;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_active 12 4957 4969
66 4957 return $self->active;
67 }
68 ▶sub multiple { }
Subroutine multiple: called CC=1 SCAR=0.0
Pod undocumented
69 2 ▶sub is_inactive { ! $_[0]->is_active }
Subroutine is_inactive: called CC=1 SCAR=0.0
Pod documented
70 93 has 'disabled' => ( is => 'rw', default => 0 );
71 93 has 'no_update' => ( is => 'rw', default => 0 );
72 93 has 'writeonly' => ( is => 'rw', default => 0 );
73 93 has 'is_contains' => ( is => 'rw' );
74 593 ▶has 'apply' => ( is => 'rw', default => sub {[]} ); # for field defnitions
Subroutine __ANON__: called CC=1 SCAR=0.0
75 0 ▶sub has_apply { return scalar @{$_[0]->{apply}} }
Subroutine has_apply: not called CC=1 SCAR=6.9
Pod undocumented
76 93 has 'base_apply' => ( is => 'rw', builder => 'build_base_apply' ); # for field classes
77 638 ▶sub build_base_apply {[]}
Subroutine build_base_apply: called CC=1 SCAR=0.0
Pod undocumented
78 0 ▶sub has_base_apply { return scalar @{$_[0]->{base_apply}} }
Subroutine has_base_apply: not called CC=1 SCAR=6.9
Pod undocumented
79 638 ▶has 'trim' => ( is => 'rw', default => sub { *default_trim } );
Subroutine __ANON__: called CC=1 SCAR=0.0
80 sub default_trim {
81 525 ▶ my $value = shift;
Subroutine default_trim: called CC=7 SCAR=19.5
Pod undocumented
82 525 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 523 525
83 523 ▶ my @values = ref $value eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 41 482 523
84 523 for (@values) {
85 561 ▶ next if ref $_ or !defined;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ or not defined $_ 83 478 561
Condition100%3/3
ref $_ or not defined $_
l!l&&r!l&&!r
812478
MC/DC100%2/2
ref $_ or not defined $_
ref $_ not defined $_
Truth table100%3/3
A or not(B)
A= ref $_B= not defined $_
ABresult
1X1
011
000
86 478 s/^\s+//;
87 478 s/\s+$//;
88 }
89 523 ▶ return ref $value eq 'ARRAY' ? \@values : $values[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 41 482 523
90 }
91 ▶sub has_fields { } # compound fields will override
Subroutine has_fields: called CC=1 SCAR=0.0
Pod undocumented
92 93 has 'methods' => ( is => 'rw', isa => HashRef, builder => 'build_methods', trigger => 1 );
93 642 ▶sub build_methods {{}}
Subroutine build_methods: called CC=1 SCAR=0.0
Pod undocumented
94 sub _trigger_methods {
95 6 ▶ my ( $self, $new_methods ) = @_;
Subroutine _trigger_methods: called CC=1 SCAR=0.0
96 6 my $base_methods = $self->build_methods;
97 6 my $methods = merge($new_methods, $base_methods);
98 6 $self->{methods} = $methods;
99  
100 }
101 sub get_method {
102 1545 ▶ my ( $self, $meth_name ) = @_;
Subroutine get_method: called CC=1 SCAR=0.0
Pod undocumented
103 1545 return $self->{methods}->{$meth_name};
104 }
105  
106 93 has 'validate_when_empty' => ( is => 'rw' );
107 93 has 'not_nullable' => ( is => 'rw' );
108 ▶sub is_repeatable {}
Subroutine is_repeatable: called CC=1 SCAR=0.0
Pod undocumented
109 ▶sub is_compound {}
Subroutine is_compound: called CC=1 SCAR=0.0
Pod undocumented
110 128 ▶sub is_form {0}
Subroutine is_form: called CC=1 SCAR=0.0
Pod undocumented
111 289 ▶sub no_fif {0}
Subroutine no_fif: called CC=1 SCAR=0.0
Pod undocumented
112  
113 around BUILDARGS => sub {
114 641 ▶ my ( $orig, $class, %field_attr ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
115  
116 641 munge_field_attr(\%field_attr);
117  
118 641 return $class->$orig(%field_attr);
119 93 };
120  
121  
122 sub BUILD {
123 641 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
124  
125 641 ▶ if ( $self->form ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form) { } 593 48 641
126 # To avoid memory cycles it needs to be weakened when
127 # it's set through a form.
128 593 weaken($self->{localizer});
129 593 weaken($self->{renderer});
130 }
131 else {
132 # Vivify. This would generally only happen in a standalone field, in tests.
133 48 $self->localizer;
134 48 $self->renderer;
135 }
136  
137 641 $self->_install_methods;
138 }
139  
140 sub _install_methods {
141 641 ▶ my $self = shift;
Subroutine _install_methods: called CC=5 SCAR=16.1
142  
143 641 ▶ if ( $self->form ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form) 593 48 641
144 593 my $suffix = $self->convert_full_name($self->full_name);
145 593 foreach my $prefix ( 'validate', 'default' ) {
146 1186 ▶ next if exists $self->methods->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->methods->{$prefix} 1 1185 1186
147 1185 my $meth_name = "${prefix}_$suffix";
148 1185 ▶ if ( my $meth = $self->form->can($meth_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meth = $self->form->can($meth_name)) 19 1166 1185
149 my $wrap_sub = sub {
150 26 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
151 26 return $self->form->$meth($self);
152 19 };
153 19 $self->{methods}->{$prefix} = $wrap_sub;
154 }
155 }
156 }
157 }
158  
159  
160 sub fif {
161 427 ▶ my $self = shift;
Subroutine fif: called CC=6 SCAR=18.0
Pod documented
162 427 ▶ return unless $self->is_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_active 0 427 427
163 427 ▶ return '' if $self->password;
Branch: 1 branch
BranchTrueFalseTotal
if $self->password 0 427 427
164 427 ▶ return $self->input if $self->has_input;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_input 219 208 427
165 208 ▶ if ( $self->has_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_value) 122 86 208
166 122 my $value = $self->value;
167 122 ▶ $value = $self->transform_value_to_fif->($self, $value) if $self->has_transform_value_to_fif;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_transform_value_to_fif 5 117 122
168 122 return $value;
169 }
170 86 return '';
171 }
172  
173  
174 sub full_name {
175 2033 ▶ my $field = shift;
Subroutine full_name: called CC=4 SCAR=13.9
Pod documented
176  
177 2033 my $name = $field->name;
178 2033 my $parent_name;
179 # field should always have a parent unless it's a standalone field test
180 2033 ▶ if ( $field->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->parent) 2018 15 2033
181 2018 $parent_name = $field->parent->full_name;
182 }
183 2033 ▶ return $name unless defined $parent_name && length $parent_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent_name and length $parent_name 1483 550 2033
Condition100%3/3
defined $parent_name and length $parent_name
!ll&&!rl&&r
151468550
MC/DC100%2/2
defined $parent_name and length $parent_name
defined $parent_name length $parent_name
Truth table100%3/3
A and B
A= defined $parent_nameB= length $parent_name
ABresult
0X0
100
111
184 550 return $parent_name . '.' . $name;
185 }
186  
187 sub full_accessor {
188 0 ▶ my $field = shift;
Subroutine full_accessor: not called CC=6 SCAR=37.4
Pod documented
189  
190 0 my $parent = $field->parent;
191 0 ▶ if( $field->is_contains ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_contains) 0 0 0
192 0 ▶ return '' unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
193 0 return $parent->full_accessor;
194 }
195 0 my $accessor = $field->accessor;
196 0 my $parent_accessor;
197 0 ▶ if ( $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 0 0 0
198 0 $parent_accessor = $parent->full_accessor;
199 }
200 0 ▶ return $accessor unless defined $parent_accessor && length $parent_accessor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent_accessor and length $parent_accessor 0 0 0
Condition0%0/3
defined $parent_accessor and length $parent_accessor
!ll&&!rl&&r
000
MC/DC0%0/2
defined $parent_accessor and length $parent_accessor
defined $parent_accessor length $parent_accessor
Truth table0%0/3
A and B
A= defined $parent_accessorB= length $parent_accessor
ABresult
0X0
100
111
201 0 return $parent_accessor . '.' . $accessor;
202 }
203  
204  
205 #====================
206 # Localization
207 #====================
208  
209 sub localize {
210 99 ▶ my ( $self, @message ) = @_;
Subroutine localize: called CC=1 SCAR=0.0
Pod undocumented
211 99 return $self->localizer->loc_($message[0]);
212 }
213  
214 93 has 'language' => ( is => 'rw', lazy => 1, builder => 'build_language' );
215 33 ▶sub build_language { 'en' }
Subroutine build_language: called CC=1 SCAR=0.0
Pod undocumented
216 93 has 'localizer' => (
217 is => 'rw', lazy => 1, builder => 'build_localizer',
218 );
219 sub build_localizer {
220 33 ▶ my $self = shift;
Subroutine build_localizer: called CC=1 SCAR=0.0
Pod undocumented
221 33 return Data::MuForm::Localizer->new(
222 language => $self->language,
223 );
224 }
225  
226 #====================
227 # Rendering
228 #====================
229 93 has 'label' => ( is => 'rw', lazy => 1, builder => 'build_label' );
230 sub build_label {
231 101 ▶ my $self = shift;
Subroutine build_label: called CC=2 SCAR=6.9
Pod documented
232 101 ▶ if ( my $meth = $self->get_method('build_label' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meth = $self->get_method("build_label")) 1 100 101
233 1 return $meth->($self);
234 }
235 100 my $label = $self->name;
236 100 $label =~ s/_/ /g;
237 100 $label = ucfirst($label);
238 100 return $label;
239 }
240 sub loc_label {
241 99 ▶ my $self = shift;
Subroutine loc_label: called CC=1 SCAR=0.0
Pod undocumented
242 99 return $self->localize($self->label);
243 }
244 93 has 'form_element' => ( is => 'rw', lazy => 1, builder => 'build_form_element' );
245 65 ▶sub build_form_element { 'input' }
Subroutine build_form_element: called CC=1 SCAR=0.0
Pod undocumented
246 93 has 'input_type' => ( is => 'rw', lazy => 1, builder => 'build_input_type' );
247 20 ▶sub build_input_type { 'text' }
Subroutine build_input_type: called CC=1 SCAR=0.0
Pod undocumented
248  
249 # could have everything in one big "pass to the renderer" hash?
250 93 has 'layout' => ( is => 'rw' );
251 93 has 'layout_group' => ( is => 'rw' );
252 93 has 'order' => ( is => 'rw', default => 0 );
253 93 has 'html5_input_type' => ( is => 'rw', predicate => 'has_html5_input_type' );
254  
255 sub base_render_args {
256 87 ▶ my $self = shift;
Subroutine base_render_args: called CC=3 SCAR=11.0
Pod undocumented
257 87 ▶ my $args = {
Condition50%1/2
$self->errors || []
l!l
870
MC/DC0%0/1
$self->errors || []
$self->errors
Truth table50%1/2
A
A= $self->errors
Aresult
11
00
258 name => $self->prefixed_name,
259 field_name => $self->name,
260 type => $self->type,
261 form_element => $self->form_element,
262 input_type => $self->input_type,
263 id => $self->id,
264 label => $self->loc_label,
265 required => $self->required,
266 errors => $self->errors || [],
267 fif => $self->fif,
268 layout_type => 'standard',
269 is_contains => $self->is_contains,
270 };
271 87 ▶ $args->{html5_input_type} = $self->html5_input_type if $self->has_html5_input_type;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_html5_input_type 7 80 87
272 87 return $args;
273 }
274  
275 93 has 'render_args' => ( is => 'rw', lazy => 1, isa => HashRef, builder => 'build_render_args' );
276 62 ▶sub build_render_args {{}}
Subroutine build_render_args: called CC=1 SCAR=0.0
Pod undocumented
277 # this is really just here for testing fields. If you want to test a custom
278 # renderer in a field, pass it in.
279 93 has 'renderer' => (
280 is => 'rw', lazy => 1,
281 builder => 'build_renderer',
282 );
283 sub build_renderer {
284 33 ▶ my $self = shift;
Subroutine build_renderer: called CC=1 SCAR=0.0
Pod undocumented
285 33 require Data::MuForm::Renderer::Base;
286 33 return Data::MuForm::Renderer::Base->new( localizer => $self->localizer );
287 }
288  
289 sub get_render_args {
290 87 ▶ my ( $self, %args ) = @_;
Subroutine get_render_args: called CC=1 SCAR=0.0
Pod undocumented
291 87 my $render_args = merge( $self->render_args, $self->base_render_args );
292 87 $render_args = merge( \%args, $render_args );
293 87 return $render_args;
294 }
295  
296 sub render {
297 61 ▶ my ( $self, $rargs ) = @_;
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
298 61 munge_render_field_attr($rargs);
299 61 my $render_args = $self->get_render_args(%$rargs);
300 61 return $self->renderer->render_field($render_args);
301 }
302  
303 sub render_element {
304 9 ▶ my ( $self, $rargs ) = @_;
Subroutine render_element: called CC=2 SCAR=7.0
Pod undocumented
305 9 my $args = { element_attr => $rargs };
306 9 my $do_errors = delete $rargs->{do_errors};
307 9 ▶ $args->{do_errors} = defined $do_errors ? $do_errors : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $do_errors ? : 0 9 9
308 9 my $render_args = $self->get_render_args(%$args);
309 9 return $self->renderer->render_element($render_args);
310 }
311  
312 sub render_errors {
313 1 ▶ my ( $self, $rargs ) = @_;
Subroutine render_errors: called CC=1 SCAR=0.0
Pod undocumented
314 1 my $render_args = $self->get_render_args( error_attr => $rargs );
315 1 return $self->renderer->render_errors($render_args);
316 }
317  
318 sub render_label {
319 1 ▶ my ( $self, $rargs, @args ) = @_;
Subroutine render_label: called CC=2 SCAR=7.0
Pod undocumented
320 1 my $render_args = $self->get_render_args( label_attr => $rargs );
321 1 ▶ $self->form->render_hook($render_args) if $self->form;
Branch: 1 branch
BranchTrueFalseTotal
if $self->form 1 0 1
322 1 return $self->renderer->render_label($render_args, @args);
323 }
324  
325  
326 #===================
327 # Errors
328 #===================
329  
330 # handles message with and without variables
331 sub add_error {
332 109 ▶ my ( $self, @message ) = @_;
Subroutine add_error: called CC=2 SCAR=6.9
Pod documented
333 109 my $out;
334 109 ▶ if ( $message[0] !~ /{/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $message[0] =~ /{/) { } 72 37 109
335 72 $out = $self->localizer->loc_($message[0]);
336 }
337 else {
338 37 $out = $self->localizer->loc_x(@message);
339 }
340 109 return $self->push_error($out);
341 }
342  
343 sub add_error_px {
344 0 ▶ my ( $self, @message ) = @_;
Subroutine add_error_px: not called CC=1 SCAR=6.9
Pod undocumented
345 0 my $out = $self->localizer->loc_px(@message);
346 0 return $self->push_error($out);;
347 }
348  
349 sub add_error_nx {
350 1 ▶ my ( $self, @message ) = @_;
Subroutine add_error_nx: called CC=1 SCAR=0.0
Pod undocumented
351 1 my $out = $self->localizer->loc_nx(@message);
352 1 return $self->push_error($out);
353 }
354  
355 sub add_error_npx {
356 0 ▶ my ( $self, @message ) = @_;
Subroutine add_error_npx: not called CC=1 SCAR=6.9
Pod undocumented
357 0 my $out = $self->localizer->loc_npx(@message);
358 0 return $self->push_error($out);;
359 }
360  
361  
362  
363 sub push_error {
364 110 ▶ my $self = shift;
Subroutine push_error: called CC=2 SCAR=6.9
Pod documented
365 110 push @{$self->{errors}}, @_;
366 110 ▶ if ( $self->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parent) 65 45 110
367 65 $self->parent->propagate_error($self);
368 }
369 }
370  
371 7 ▶sub clear { shift->clear_data }
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
372  
373 #===================
374 # Transforms
375 #===================
376  
377 # these are all coderefs
378 93 has 'transform_param_to_input' => ( is => 'rw', predicate => 'has_transform_param_to_input' );
379 93 has 'transform_input_to_value' => ( is => 'rw', predicate => 'has_transform_input_to_value' );
380 93 has 'transform_default_to_value' => ( is => 'rw', predicate => 'has_transform_default_to_value' );
381 93 has 'transform_value_after_validate' => ( is => 'rw', predicate => 'has_transform_value_after_validate' );
382 93 has 'transform_value_to_fif' => ( is => 'rw', predicate => 'has_transform_value_to_fif' );
383  
384 #====================================================================
385 # Validation
386 #====================================================================
387  
388 93 has 'required' => ( is => 'rw', default => 0 );
389 93 has 'required_when' => ( is => 'rw', isa => HashRef, predicate => 'has_required_when' );
390 93 has 'unique' => ( is => 'rw', predicate => 'has_unique' );
391 1 ▶sub validated { !$_[0]->has_errors && $_[0]->has_input }
Subroutine validated: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
not $_[0]->has_errors and $_[0]->has_input 1 0 1
392 ▶sub normalize_input { } # intended for field classes, to make sure input is in correct form, mostly multiple or not
Subroutine normalize_input: called CC=1 SCAR=0.0
Pod undocumented
393  
394 sub input_defined {
395 585 ▶ my ($self) = @_;
Subroutine input_defined: called CC=2 SCAR=7.1
Pod undocumented
396 585 ▶ return unless $self->has_input;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_input 0 585 585
397 585 return has_some_value( $self->input );
398 }
399  
400 sub has_some_value {
401 682 ▶ my $x = shift;
Subroutine has_some_value: called CC=11 SCAR=25.9
Pod undocumented
402  
403 682 ▶ return unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 4 678 682
404 678 ▶ return $x =~ /\S/ if !ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 584 94 678
405 94 ▶ if ( ref $x eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x eq "ARRAY") 24 70 94
406 24 for my $elem (@$x) {
407 25 ▶ return 1 if has_some_value($elem);
Branch: 1 branch
BranchTrueFalseTotal
if has_some_value($elem) 21 4 25
408 }
409 3 return 0;
410 }
411 70 ▶ if ( ref $x eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x eq "HASH") 70 0 70
412 70 for my $key ( keys %$x ) {
413 72 ▶ return 1 if has_some_value( $x->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
if has_some_value($x->{$key}) 63 9 72
414 }
415 7 return 0;
416 }
417 0 ▶ return 1 if blessed($x); # true if blessed, otherwise false
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($x) 0 0 0
418 0 ▶ return 1 if ref( $x );
Branch: 1 branch
BranchTrueFalseTotal
if ref $x 0 0 0
419 0 return;
420 }
421  
422  
423  
424 ▶sub base_validate { }
Subroutine base_validate: not called CC=1 SCAR=0.0
Pod undocumented
425 442 ▶sub validate {1}
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
426  
427 sub field_validate {
428 610 ▶ my $self = shift;
Subroutine field_validate: called CC=25 SCAR=32.3
Pod undocumented
429  
430 610 ▶ return if ( $self->has_fields && $self->skip_fields_without_input && ! $self->has_input );
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_fields and $self->skip_fields_without_input and not $self->has_input 0 610 610
Condition50%3/6
$self->has_fields and $self->skip_fields_without_input and not $self->has_input
!ll&&!rl&&r
61000
$self->has_fields and $self->skip_fields_without_input
!ll&&!rl&&r
523870
MC/DC0%0/3
$self->has_fields and $self->skip_fields_without_input and not $self->has_input
$self->has_fields $self->skip_fields_without_input not $self->has_input
Truth table50%2/4
A and B and not(C)
A= $self->has_fieldsB= $self->skip_fields_without_inputC= not $self->has_input
ABCresult
0XX0
10X0
1100
1111
431  
432 610 $self->normalize_input;
433  
434 610 my $continue_validation = 1;
435 610 ▶ if ( ( $self->required ||
Branch: 4 branches
BranchTrueFalseTotal
if ($self->required || $self->has_required_when && $self->match_when($self->required_when) and not $self->has_input && $self->input_defined) { } 24 586 610
elsif ($self->is_repeatable) { } 28 558 586
elsif (not $self->has_input) { } 39 519 558
elsif (not $self->input_defined) { } 19 500 519
Condition100%12/12
$self->required || $self->has_required_when && $self->match_when($self->required_when) and not $self->has_input && $self->input_defined
!ll&&!rl&&r
5365024
$self->required or $self->has_required_when and $self->match_when($self->required_when)
l!l&&r!l&&!r
7188451
$self->has_required_when and $self->match_when($self->required_when)
!ll&&!rl&&r
53333
$self->has_input && $self->input_defined
!ll&&!rl&&r
81650
MC/DC100%5/5
$self->required || $self->has_required_when && $self->match_when($self->required_when) and not $self->has_input && $self->input_defined
$self->required $self->has_required_when $self->match_when($self->required_when) $self->has_input $self->input_defined
Truth table75%6/8
A or B and C and not(D && E)
A= $self->requiredB= $self->has_required_whenC= $self->match_when($self->required_when)D= $self->has_inputE= $self->input_defined
ABCDEresult
00XXX0
010XX0
1XX110
011110
1XX0X1
1XX101
0110X1
011101
436 ( $self->has_required_when && $self->match_when($self->required_when) ) ) &&
437 ( ! $self->has_input || ! $self->input_defined )) {
438 24 $self->add_error( $self->get_message('required'), field_label => $self->label );
439 24 ▶ if( $self->has_input ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_input) 16 8 24
440 16 ▶ $self->not_nullable ? $self->value($self->input) : $self->value(undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->not_nullable ? : 0 16 16
441 }
442  
443 24 $continue_validation = 0;
444 }
445 elsif ( $self->is_repeatable ) { }
446 elsif ( !$self->has_input ) {
447 39 $continue_validation = 0;
448 }
449 elsif ( !$self->input_defined ) {
450 19 ▶ if ( $self->not_nullable ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->not_nullable) { } 3 16 19
elsif ($self->no_value_if_empty or $self->is_contains) { } 2 14 16
451 3 $self->value($self->input);
452 # handles the case where a compound field value needs to have empty subfields
453 3 ▶ $continue_validation = 0 unless $self->is_compound;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_compound 3 0 3
454 }
455 ▶ elsif ( $self->no_value_if_empty || $self->is_contains ) {
Condition100%3/3
$self->no_value_if_empty or $self->is_contains
l!l&&r!l&&!r
1114
MC/DC100%2/2
$self->no_value_if_empty or $self->is_contains
$self->no_value_if_empty $self->is_contains
Truth table100%3/3
A or B
A= $self->no_value_if_emptyB= $self->is_contains
ABresult
1X1
011
000
456 2 $continue_validation = 0;
457 }
458 else {
459 14 $self->value(undef);
460 14 $continue_validation = 0;
461 }
462 }
463 610 ▶ return if ( !$continue_validation && !$self->validate_when_empty );
Branch: 1 branch
BranchTrueFalseTotal
unless $continue_validation or $self->validate_when_empty 82 528 610
Condition66%2/3
$continue_validation or $self->validate_when_empty
l!l&&r!l&&!r
528082
MC/DC50%1/2
$continue_validation or $self->validate_when_empty
$continue_validation $self->validate_when_empty
Truth table66%2/3
A or B
A= $continue_validationB= $self->validate_when_empty
ABresult
1X1
011
000
464  
465  
466 528 ▶ if ( $self->has_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_fields) { } 75 453 528
467 75 $self->fields_validate;
468 }
469 else {
470 453 my $input = $self->input;
471 453 ▶ $input = $self->transform_input_to_value->($self, $input) if $self->has_transform_input_to_value;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_transform_input_to_value 8 445 453
472 453 $self->value($input);
473 }
474  
475 528 ▶ $self->value( $self->trim->($self->value) ) if $self->trim;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trim 527 1 528
476  
477 528 $self->validate($self->value); # this is field class validation. Do it before the other validations.
478  
479 528 $self->apply_actions; # this could be either from the field definitions or from a custom field
480  
481 # this is validate_<field name> or methods->{validate => ...} validation
482 528 ▶ if ( my $meth = $self->get_method('validate') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meth = $self->get_method("validate")) 19 509 528
483 19 $meth->($self);
484 }
485  
486 528 ▶ if ( $self->has_transform_value_after_validate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_transform_value_after_validate) 6 522 528
487 6 my $value = $self->value;
488 6 $value = $self->transform_value_after_validate->($self, $value);
489 6 $self->value($value);
490 }
491  
492 528 return ! $self->has_errors;
493 }
494  
495 sub transform_and_set_input {
496 416 ▶ my ( $self, $input ) = @_;
Subroutine transform_and_set_input: called CC=2 SCAR=6.9
Pod undocumented
497 416 ▶ $input = $self->transform_param_to_input->($self, $input) if $self->has_transform_param_to_input;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_transform_param_to_input 4 412 416
498 416 $self->input($input);
499 }
500  
501  
502 sub apply_actions {
503 528 ▶ my $self = shift;
Subroutine apply_actions: called CC=29 SCAR=35.1
Pod undocumented
504  
505 528 my $error_message;
506 local $SIG{__WARN__} = sub {
507 2 ▶ my $error = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
508 2 $error_message = $error;
509 2 return 1;
510 528 };
511  
512 my $is_type = sub {
513 229 ▶ my $class = blessed shift or return;
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
my $class = builtin::blessed(shift()) or return 46 183 229
514 183 ▶ return $class eq 'MooseX::Types::TypeDecorator' || $class->isa('Type::Tiny');
Condition33%1/3
$class eq 'MooseX::Types::TypeDecorator' || $class->isa('Type::Tiny')
l!l&&r!l&&!r
01830
MC/DC0%0/2
$class eq 'MooseX::Types::TypeDecorator' || $class->isa('Type::Tiny')
$class eq 'MooseX::Types::TypeDecorator' $class->isa('Type::Tiny')
Truth table33%1/3
A || B
A= $class eq 'MooseX::Types::TypeDecorator'B= $class->isa('Type::Tiny')
ABresult
1X1
011
000
515 528 };
516  
517 528 my @actions;
518 528 push @actions, @{ $self->base_apply }, @{ $self->apply };
519 528 for my $action ( @actions ) {
520 134 $error_message = undef;
521 # the first time through value == input
522 134 my $value = $self->value;
523 134 my $new_value = $value;
524 # Moose constraints
525 134 ▶ if ( !ref $action || $is_type->($action) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $action or &$is_type($action)) 88 46 134
Condition66%2/3
not ref $action or &$is_type($action)
l!l&&r!l&&!r
08846
MC/DC50%1/2
not ref $action or &$is_type($action)
not ref $action &$is_type($action)
Truth table66%2/3
not(A) or B
A= not ref $actionB= &$is_type($action)
ABresult
1X1
011
000
526 88 $action = { type => $action };
527 }
528 134 ▶ if ( my $when = $action->{when} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $when = $action->{"when"}) 6 128 134
529 6 ▶ next unless $self->match_when($when);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->match_when($when) 3 3 6
530 }
531 131 ▶ if ( exists $action->{type} ) {
Branch: 5 branches
BranchTrueFalseTotal
if (exists $action->{'type'}) { } 95 36 131
elsif (ref $action->{'check'} eq 'CODE') { } 11 25 36
elsif (ref $action->{'check'} eq 'Regexp') { } 6 19 25
elsif (ref $action->{'check'} eq 'ARRAY') { } 5 14 19
elsif (ref $action->{'transform'} eq 'CODE') { } 14 0 14
532 95 my $tobj;
533 95 ▶ if ( $is_type->($action->{type}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$is_type($action->{'type'})) { } 95 0 95
534 95 $tobj = $action->{type};
535 }
536 else {
537 0 my $type = $action->{type};
538 0 ▶ $tobj = Moose::Util::TypeConstraints::find_type_constraint($type) or
Branch: 1 branch
BranchTrueFalseTotal
$tobj = Moose::Util::TypeConstraints::find_type_constraint($type) or die "Cannot find type constraint $type" 0 0 0
539 die "Cannot find type constraint $type";
540 }
541 95 ▶ if ( $tobj->has_coercion && $tobj->validate($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tobj->has_coercion and $tobj->validate($value)) 14 81 95
Condition100%3/3
$tobj->has_coercion and $tobj->validate($value)
!ll&&!rl&&r
79214
MC/DC100%2/2
$tobj->has_coercion and $tobj->validate($value)
$tobj->has_coercion $tobj->validate($value)
Truth table100%3/3
A and B
A= $tobj->has_coercionB= $tobj->validate($value)
ABresult
0X0
100
111
542 14 eval { $new_value = $tobj->coerce($value) };
543 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 14 14
544 0 ▶ if ( $tobj->has_message ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tobj->has_message) { } 0 0 0
545 0 $error_message = $tobj->message->($value);
546 }
547 else {
548 0 $error_message = $@;
549 }
550 }
551 else {
552 14 $self->value($new_value);
553 }
554  
555 }
556 95 ▶ $error_message ||= $tobj->validate($new_value);
Condition66%2/3
$error_message ||= $tobj->validate($new_value)
l!l&&r!l&&!r
03758
MC/DC50%1/2
$error_message ||= $tobj->validate($new_value)
$error_message $tobj->validate($new_value)
Truth table66%2/3
A ||= B
A= $error_messageB= $tobj->validate($new_value)
ABresult
1X1
011
000
557 }
558 # now maybe: http://search.cpan.org/~rgarcia/perl-5.10.0/pod/perlsyn.pod#Smart_matching_in_detail
559 # actions in a hashref
560 elsif ( ref $action->{check} eq 'CODE' ) {
561 11 ▶ if ( !$action->{check}->($value, $self) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action->{"check"}->($value, $self)) 3 8 11
562 3 $error_message = $self->get_message('wrong_value');
563 }
564 }
565 elsif ( ref $action->{check} eq 'Regexp' ) {
566 6 ▶ if ( $value !~ $action->{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$action->{"check"}/) 5 1 6
567 5 $error_message = [$self->get_message('no_match'), 'value', $value];
568 }
569 }
570 elsif ( ref $action->{check} eq 'ARRAY' ) {
571 5 ▶ if ( !grep { $value eq $_ } @{ $action->{check} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$value eq $_;} @{$$action{"check"};}) 4 1 5
572 4 $error_message = [$self->get_message('not_allowed'), 'value', $value];
573 }
574 }
575 elsif ( ref $action->{transform} eq 'CODE' ) {
576 14 $new_value = eval {
577 93 ▶ no warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
578 14 $action->{transform}->($value, $self);
579 };
580 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 14 14
581 0 ▶ $error_message = $@ || $self->get_message('error_occurred');
Condition0%0/3
$@ || $self->get_message('error_occurred')
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $self->get_message('error_occurred')
$@ $self->get_message('error_occurred')
Truth table0%0/3
A || B
A= $@B= $self->get_message('error_occurred')
ABresult
1X1
011
000
582 }
583 else {
584 14 $self->value($new_value);
585 }
586 }
587 131 ▶ if ( defined $error_message ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error_message) 50 81 131
588 50 ▶ my @message = ref $error_message eq 'ARRAY' ? @$error_message : ($error_message);
Branch: 1 branch
BranchTrueFalseTotal
ref $error_message eq 'ARRAY' ? : 9 41 50
589 50 ▶ if ( defined $action->{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $action->{"message"}) 13 37 50
590 13 my $act_msg = $action->{message};
591 13 ▶ if ( ref $act_msg eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $act_msg eq "CODE") 3 10 13
592 3 $act_msg = $act_msg->($value, $self, $error_message);
593 }
594 13 ▶ if ( ref $act_msg eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $act_msg eq 'ARRAY') { } 3 10 13
elsif (ref \$act_msg eq 'SCALAR') { } 10 0 10
595 3 @message = @{$act_msg};
596 }
597 elsif ( ref \$act_msg eq 'SCALAR' ) {
598 10 @message = ($act_msg);
599 }
600 }
601 50 $self->add_error(@message);
602 }
603 }
604 }
605  
606 sub match_when {
607 12 ▶ my ( $self, $when ) = @_;
Subroutine match_when: called CC=11 SCAR=24.1
Pod undocumented
608  
609 12 my $matched = 0;
610 12 foreach my $key ( keys %$when ) {
611 12 my $check_against = $when->{$key};
612 12 my $from_form = ( $key =~ /^\+/ );
613 12 $key =~ s/^\+//;
614 12 ▶ my $field = $from_form ? $self->form->field($key) : $self->parent->subfield( $key );
Branch: 1 branch
BranchTrueFalseTotal
$from_form ? : 4 8 12
615 12 ▶ unless ( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 12 12
616 0 warn "field '$key' not found processing 'when' for '" . $self->full_name . "'";
617 0 next;
618 }
619 12 ▶ my $field_fif = defined $field->fif ? $field->fif : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $field->fif ? : 12 0 12
620 12 ▶ if ( ref $check_against eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $check_against eq 'CODE') { } 4 8 12
elsif (ref $check_against eq 'ARRAY') { } 2 6 8
elsif ($check_against eq $field_fif) { } 3 3 6
621 4 ▶ $matched++
Branch: 1 branch
BranchTrueFalseTotal
if &$check_against($field_fif, $self) 2 2 4
622 if $check_against->($field_fif, $self);
623 }
624 elsif ( ref $check_against eq 'ARRAY' ) {
625 2 foreach my $value ( @$check_against ) {
626 6 ▶ $matched++ if ( $value eq $field_fif );
Branch: 1 branch
BranchTrueFalseTotal
if $value eq $field_fif 1 5 6
627 }
628 }
629 elsif ( $check_against eq $field_fif ) {
630 3 $matched++;
631 }
632 else {
633 3 $matched = 0;
634 3 last;
635 }
636 }
637 12 return $matched;
638 }
639  
640 #====================================================================
641 # Filling
642 #====================================================================
643  
644 sub fill_from_params {
645 410 ▶ my ( $self, $input, $exists ) = @_;
Subroutine fill_from_params: called CC=4 SCAR=13.9
Pod undocumented
646  
647 410 $self->filled_from('params');
648 410 ▶ if ( $exists ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($exists) { } 341 69 410
elsif ($self->disabled) { } 1 68 69
elsif ($self->has_input_without_param) { } 20 48 68
649 341 $self->transform_and_set_input($input);
650 }
651 elsif ( $self->disabled ) {
652 }
653 elsif ( $self->has_input_without_param ) {
654 20 $self->transform_and_set_input($self->input_without_param);
655 }
656 410 return;
657 }
658  
659 sub fill_from_object {
660 210 ▶ my ( $self, $value ) = @_;
Subroutine fill_from_object: called CC=3 SCAR=11.4
Pod undocumented
661  
662 210 $self->filled_from('object');
663 210 $self->value($value);
664  
665 210 ▶ if ( $self->form ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form) { } 210 0 210
666 210 $self->form->init_value( $self, $value );
667 }
668 else {
669 0 $self->init_value($value);
670 #$result->_set_value($value);
671 }
672 210 ▶ $self->value(undef) if $self->writeonly;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writeonly 0 210 210
673  
674 210 return;
675 }
676  
677 sub fill_from_fields {
678 598 ▶ my ( $self ) = @_;
Subroutine fill_from_fields: called CC=5 SCAR=16.2
Pod undocumented
679  
680 598 ▶ if ( my @values = $self->get_default_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@values) = $self->get_default_value) 36 562 598
681 36 ▶ if ( $self->has_transform_default_to_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_transform_default_to_value) 2 34 36
682 2 @values = $self->transform_default_to_value->($self, @values);
683 }
684 36 ▶ my $value = @values > 1 ? \@values : shift @values;
Branch: 1 branch
BranchTrueFalseTotal
@values > 1 ? : 0 36 36
685 36 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 36 0 36
686 36 $self->init_value($value);
687 36 $self->value($value);
688 }
689 }
690 598 return;
691 }
692  
693  
694 sub clear_data {
695 294 ▶ my $self = shift;
Subroutine clear_data: called CC=1 SCAR=0.0
Pod undocumented
696 294 $self->clear_input;
697 294 $self->clear_value;
698 294 $self->clear_errors;
699 294 $self->_clear_active;
700 294 $self->clear_filled_from;
701 }
702  
703 sub get_default_value {
704 726 ▶ my $self = shift;
Subroutine get_default_value: called CC=3 SCAR=11.0
Pod undocumented
705 726 ▶ if ( my $meth = $self->get_method('default') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $meth = $self->get_method('default')) { } 10 716 726
elsif (defined $self->default) { } 37 679 716
706 10 return $meth->($self);
707 }
708 elsif ( defined $self->default ) {
709 37 return $self->default;
710 }
711 679 return;
712 }
713  
714  
715 #====================================================================
716 # Messages
717 #====================================================================
718  
719 637 ▶has 'messages' => ( is => 'rw', isa => HashRef, default => sub {{}} );
Subroutine __ANON__: called CC=1 SCAR=0.0
720 4 ▶sub _get_field_message { my ($self, $msg) = @_; return $self->{messages}->{$msg}; }
Subroutine _get_field_message: called CC=1 SCAR=0.0
721 66 ▶sub _has_field_message { my ($self, $msg) = @_; exists $self->{messages}->{$msg}; }
Subroutine _has_field_message: called CC=1 SCAR=0.0
722 0 ▶sub set_message { my ($self, $msg, $value) = @_; $self->{messages}->{$msg} = $value; }
Subroutine set_message: not called CC=1 SCAR=6.9
Pod undocumented
723  
724  
725 93 our $class_messages = {
726 'field_invalid' => 'field is invalid',
727 'range_too_low' => 'Value must be greater than or equal to [_1]',
728 'range_too_high' => 'Value must be less than or equal to [_1]',
729 'range_incorrect' => 'Value must be between {start} and {end}',
730 'wrong_value' => 'Wrong value',
731 'no_match' => '[_1] does not match',
732 'not_allowed' => '[_1] not allowed',
733 'error_occurred' => 'error occurred',
734 'required' => "'{field_label}' field is required",
735 'unique' => 'Duplicate value for [_1]', # this is used in the DBIC model
736 };
737  
738 sub get_class_messages {
739 61 ▶ my $self = shift;
Subroutine get_class_messages: called CC=1 SCAR=0.0
Pod undocumented
740 61 my $messages = { %$class_messages };
741 61 return $messages;
742 }
743  
744 sub get_message {
745 66 ▶ my ( $self, $msg ) = @_;
Subroutine get_message: called CC=4 SCAR=13.9
Pod undocumented
746  
747 # first look in messages set on individual field
748 66 ▶ return $self->_get_field_message($msg)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_field_message($msg) 4 62 66
749 if $self->_has_field_message($msg);
750 # then look at form messages
751 62 ▶ return $self->form->_get_form_message($msg)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_form and $self->form->_has_form_message($msg) 1 61 62
Condition100%3/3
$self->has_form and $self->form->_has_form_message($msg)
!ll&&!rl&&r
24371
MC/DC100%2/2
$self->has_form and $self->form->_has_form_message($msg)
$self->has_form $self->form->_has_form_message($msg)
Truth table100%3/3
A and B
A= $self->has_formB= $self->form->_has_form_message($msg)
ABresult
0X0
100
111
752 if $self->has_form && $self->form->_has_form_message($msg);
753 # then look for messages up through inherited field classes
754 61 return $self->get_class_messages->{$msg};
755 }
756 sub all_messages {
757 0 ▶ my $self = shift;
Subroutine all_messages: not called CC=4 SCAR=30.0
Pod undocumented
758 0 ▶ my $form_messages = $self->has_form ? $self->form->messages : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->has_form ? : 0 0 0
759 0 ▶ my $field_messages = $self->messages || {};
Condition0%0/2
$self->messages || {}
l!l
00
MC/DC0%0/1
$self->messages || {}
$self->messages
Truth table0%0/2
A
A= $self->messages
Aresult
11
00
760 0 ▶ my $lclass_messages = $self->my_class_messages || {};
Condition0%0/2
$self->my_class_messages || {}
l!l
00
MC/DC0%0/1
$self->my_class_messages || {}
$self->my_class_messages
Truth table0%0/2
A
A= $self->my_class_messages
Aresult
11
00
761 0 return {%{$lclass_messages}, %{$form_messages}, %{$field_messages}};
762 }
763  
764 sub clone {
765 4892 ▶ my $self = shift;
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
766 4892 return data_clone($self);
767 }
768  
769 sub get_result {
770 2 ▶ my $self = shift;
Subroutine get_result: called CC=2 SCAR=7.0
Pod undocumented
771 2 my $result = {
772 name => $self->name,
773 full_name => $self->full_name,
774 id => $self->id,
775 label => $self->label,
776 render_args => $self->render_args,
777 fif => $self->fif,
778 };
779 2 ▶ $result->{errors} = $self->errors if $self->has_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_errors 0 2 2
780 2 return $result;
781 }
782  
783 sub convert_full_name {
784 637 ▶ my ( $self, $full_name ) = @_;
Subroutine convert_full_name: called CC=1 SCAR=0.0
Pod undocumented
785 637 $full_name =~ s/\.\d+\./_/g;
786 637 $full_name =~ s/\./_/g;
787 637 return $full_name;
788 }
789  
790  
791 93 1;
792  
793 __END__
794  
795 =pod
796  
797 =encoding UTF-8
798  
799 =head1 NAME
800  
801 Data::MuForm::Field - Base field package
802  
803 =head1 VERSION
804  
805 version 0.04
806  
807 =head1 SYNOPSIS
808  
809 Instances of Field subclasses are generally built by L<Data::MuForm>
810 from 'has_field' declarations or the field_list.
811  
812 has_field 'my_field' => ( type => 'Integer' );
813 field_list => [
814 my_field => { type => 'Integer' }
815 ]
816  
817 Fields can also be added with add_field:
818  
819 $form->add_field( name => 'my_field', type => 'Integer' );
820  
821 You can create custom field classes:
822  
823 package MyApp::Field::MyText;
824 use Moo;
825 use Data::MuForm::Meta;
826 extends 'Data::MuForm::Field::Text';
827  
828 has 'my_attribute' => ( is => 'rw' );
829  
830 sub validate { <perform validation> }
831  
832 1;
833  
834 =head1 DESCRIPTION
835  
836 This is the base class for form fields. The 'type' of a field class
837 is used in the MuForm field_list or has_field to identify which field class to
838 load from the 'field_namespace' (or directly, when prefixed with '+').
839 If the type is not specified, it defaults to Text.
840  
841 See L<Data::MuForm::Manual::Fields> for a list of the fields and brief
842 descriptions of their structure.
843  
844 =head1 NAME
845  
846 Data::MuForm::Field
847  
848 =head1 ATTRIBUTES
849  
850 =head2 Names, types, accessor
851  
852 =over
853  
854 =item name
855  
856 The name of the field. Used in the HTML form. Often a db accessor.
857 The only required attribute.
858  
859 =item type
860  
861 The class or type of the field. The 'type' of L<Data::MuForm::Field::Currency>
862 is 'Currency'.
863  
864 =item id
865  
866 The id to use when rendering. This can come from a number of different places.
867  
868 1) field definition
869 2) field 'build_id' method
870 3) form 'build_field_id' method
871 4) field prefixed_name
872  
873 =item accessor
874  
875 If the name of your field is different than your database accessor, use
876 this attribute to provide the accessor.
877  
878 =item full_name
879  
880 The name of the field with all parents:
881  
882 'event.start_date.month'
883  
884 =item full_accessor
885  
886 The field accessor with all parents.
887  
888 =item prefixed_name
889  
890 The full_name plus the prefix provided in 'field_prefix'. Useful for multiple
891 forms on the same page.
892  
893 =item input_param
894  
895 By default we expect an input parameter based on the field name. This allows
896 you to look for a different input parameter.
897  
898 =back
899  
900 =head2 Field data
901  
902 =over
903  
904 =item active, inactive, is_active, is_inactive
905  
906 Determines which fields will be processed and rendered.
907  
908 Can be changed on a process call, and cleared afterward:
909  
910 $form->process( active => [ 'foo', 'bar' ], params => $params );
911  
912 You can use the is_inactive and is_active methods to check whether this particular
913 field is active. May be necessary to use in templates if you're changing the
914 active/inactive of some fields.
915  
916 if( $form->field('foo')->is_active ) { ... }
917  
918 =item input
919  
920 The input string from the parameters passed in. This is not usually set by
921 the user.
922  
923 =item value
924  
925 The value as it would come from or go into the database, after being
926 acted on by transforms and validation code. Used to construct the
927 C<< $form->values >> hash. Before validation is performed, the input is
928 copied to the 'value', and validation and constraints should act on 'value'.
929 After validation, C<< $form->value >> will get a hashref of the values.
930  
931 See also L<Data::MuForm::Manual::Transforms>.
932  
933 =item fif
934  
935 Values used to fill in the form. Read only.
936  
937 [% form.field('title').fif %]
938  
939 =item init_value
940  
941 Initial value populated by fill_from_object. You can tell if a field
942 has changed by comparing 'init_value' and 'value'. You wouldn't normally
943 change this.
944  
945 =item input_without_param
946  
947 Input for this field if there is no param. Set by default for Checkbox,
948 and Select, since an unchecked checkbox or unselected pulldown
949 does not return a parameter.
950  
951 =back
952  
953 =head2 Form, parent, etc
954  
955 =over
956  
957 =item form
958  
959 A reference to the containing form.
960  
961 =item parent
962  
963 A reference to the parent of this field. Compound fields are the
964 parents for the fields they contain.
965  
966 =item localizer
967  
968 Set from the form when fields are created.
969  
970 =item renderer
971  
972 Set from the form when fields are created.
973  
974 =back
975  
976 =head2 Errors
977  
978 =over
979  
980 =item errors
981  
982 Returns the error list (arrayref) for the field. Also provides
983 'all_errors', 'num_errors', 'has_errors', 'push_error' and 'clear_errors'.
984 Use 'add_error' to add an error to the array if you
985 want to localize the error message, or 'push_error' to skip
986 the localization.
987  
988 =item add_error
989  
990 Add an error to the list of errors. Error message will be localized
991 using 'localize' method, and the Localizer (default is
992 Data::MuForm::Localizer, which use a gettext style .po file).
993  
994 return $field->add_error( 'bad data' ) if $bad;
995  
996 =item push_error
997  
998 Adds an error to the list of errors without localization.
999  
1000 =item error_fields
1001  
1002 The form and Compound fields will have an array of errors from the subfields.
1003  
1004 =back
1005  
1006 =head2 methods
1007  
1008 A 'methods' hashref allows setting various coderefs, 'build_id', 'build_label',
1009 'build_options', 'validate', 'default'.
1010  
1011 methods => { build_id => \&my_build_id } - coderef for constructing the id
1012 methods => { build_label => \&my_build_label } - coderef for constructing the label
1013  
1014 =over
1015  
1016 =item build_id
1017  
1018 A coderef to build the field's id. If one doesn't exist, will use a form 'build_field_id'
1019 method. Fallback is to use the field's full name.
1020  
1021 =item build_label
1022  
1023 =item build_options
1024  
1025 =item validate
1026  
1027 =item default
1028  
1029 =back
1030  
1031 =head2 render_args
1032  
1033 The 'render_args' hashref contains keys which are used in rendering, with shortcuts
1034 for easier specification in a field definition.
1035  
1036 element_attr - ea
1037 label_attr - la
1038 wrapper_attr - wa
1039 error_attr - era
1040 element_wrapper_attr - ewa
1041  
1042 has_field 'foo' => ( render_args => { element_attr => { readonly => 1, my_attr => 'abc' }} );
1043 has_field 'foo' => ( 'ra.ea' => { readonly => 1, my_attr => 'abc' } );
1044 has_field 'foo' => ( 'ra'.wa.class' => ['mb10', 'wr66'] );
1045  
1046 Note the the 'name', 'id', and 'value' of fields is set by field attributes. Though
1047 it is possible to override the id in render_args, it then won't be available for
1048 other code such as 'errors_by_id'. There is some behavior associated with the 'disabled'
1049 flag too.
1050  
1051 label - Text label for this field. Defaults to ucfirst field name.
1052 id - Used in 'id="<id>"' in HTML
1053 disabled - Boolean to set field disabled
1054  
1055 The order attribute may be used to set the order in which fields are rendered.
1056  
1057 order - Used for sorting errors and fields. Built automatically,
1058 but may also be explicitly set. Auto sequence is by 5: 5, 10, 15, etc
1059  
1060 =head2 Flags
1061  
1062 =over
1063  
1064 =item password
1065  
1066 Prevents the entered value from being displayed in the form
1067  
1068 =item writeonly
1069  
1070 The initial value is not taken from the database
1071  
1072 =item no_update
1073  
1074 Do not include this field in C<< $form->values >>, and so it won't be updated in the database.
1075  
1076 =item not_nullable
1077  
1078 Fields that contain 'empty' values such as '' are changed to undef in the validation process.
1079 If this flag is set, the value is not changed to undef. If your database column requires
1080 an empty string instead of a null value (such as a NOT NULL column), set this attribute.
1081  
1082 has_field 'description' => (
1083 type => 'TextArea',
1084 not_nullable => 1,
1085 );
1086  
1087 This attribute is also used when you want an empty array to stay an empty array and not
1088 be set to undef.
1089  
1090 It's also used when you have a compound field and you want the 'value' returned
1091 to contain subfields with undef, instead of the whole field to be undef.
1092  
1093 =back
1094  
1095 =head2 Defaults
1096  
1097 See also the documentation on L<Data::MuForm::Manual::Defaults>.
1098  
1099 =over
1100  
1101 =item default method
1102  
1103 Note: do *not* set defaults by setting the 'checked' or 'selected' attributes
1104 in options. The code will be unaware that defaults have been set.
1105  
1106 has_field 'foo' => ( methods => { default => \&my_default } );
1107 sub my_default { }
1108 OR
1109 has_field 'foo';
1110 sub default_foo { }
1111  
1112 Supply a coderef (which will be a method on the field).
1113 If not specified and a form method with a name of
1114 C<< default_<field_name> >> exists, it will be used.
1115  
1116 =item default
1117  
1118 Provide an initial value in the field declaration:
1119  
1120 has_field 'bax' => ( default => 'Default bax' );
1121  
1122 =back
1123  
1124 =head1 Constraints and Validations
1125  
1126 See also L<Data::MuForm::Manual::Validation>.
1127  
1128 =head2 Constraints set in attributes
1129  
1130 =over
1131  
1132 =item required
1133  
1134 Flag indicating whether this field must have a value
1135  
1136 =item unique
1137  
1138 For DB field - check for uniqueness. Action is performed by
1139 the DB model.
1140  
1141 =item apply
1142  
1143 Use the 'apply' keyword to specify an ArrayRef of constraints and coercions to
1144 be executed on the field at field_validate time.
1145  
1146 has_field 'test' => (
1147 apply => [ TinyType,
1148 { check => sub {...}, message => { } },
1149 { transform => sub { ... lc(shift) ... } }
1150 ],
1151 );
1152  
1153 =back
1154  
1155 =head2 messages
1156  
1157 has_field 'foo' => ( messages => { required => '...', unique => '...' } );
1158 or
1159 has_field 'foo' => ( 'msg.required' => '...' );
1160  
1161 Set messages created by MuForm by setting in the 'messages'
1162 hashref or with the 'msg.<msg_name>' shortcut. Some field subclasses have additional
1163 settable messages.
1164  
1165 required: Error message text added to errors if required field is not present.
1166 The default is "Field <field label> is required".
1167  
1168 =head2 Transforms
1169  
1170 There are a number of methods to provide finely tuned transformation of the
1171 input or value.
1172  
1173 See also L<Data::MuForm::Manual::Transforms>.
1174  
1175 =over 4
1176  
1177 =item transform_input_to_value
1178  
1179 In FH was 'inflate_method'.
1180  
1181 Transforms the string that was submitted in params (and copied to 'input') when
1182 it's stored in the 'value' attribute during validation.
1183  
1184 =item transform_value_to_fif
1185  
1186 In FH was 'deflate_method'.
1187  
1188 When you get 'fif' for the field and the 'value' is used (as opposed to input)
1189 transforms the value to a string suitable for filling in a form field.
1190  
1191 =item transform_default_to_value
1192  
1193 In FH was inflate_default_method.
1194  
1195 Transform the 'default' provided by an 'model' or 'init_values' or 'default' when it's stored
1196 in the 'value'.
1197  
1198 =item transform_value_after_validate
1199  
1200 In FH was 'deflate_value_method';
1201  
1202 Transform the value after validation has been performs, in order to return
1203 a different form in C<< $form->value >>.
1204  
1205 =item transform_param_to_input
1206  
1207 Transform the param when it's stored in 'input'. Will change what the user sees
1208 in a re-presented form.
1209  
1210 =item trim
1211  
1212 A transform to trim the field. The default 'trim' sub
1213 strips beginning and trailing spaces.
1214 Set this attribute to null to skip trimming, or supply a different
1215 sub.
1216  
1217 trim => sub {
1218 my $string = shift;
1219 <do something>
1220 return $string;
1221 }
1222  
1223 Trimming is performed before any other defined actions.
1224  
1225 =back
1226  
1227 =head1 Processing and validating the field
1228  
1229 See also L<Data::MuForm::Manual::Validation>.
1230  
1231 =head2 Validate method
1232  
1233 has_field 'foo' => ( methods => { validate => \&foo_validation } );
1234 sub foo_validation { }
1235 OR
1236 has_field 'foo';
1237 sub validate_foo { }
1238  
1239 Supply a coderef (which will be a method on the field).
1240 If not specified and a form method with a name of
1241 C<< validate_<field_name> >> exists, it will be used instead.
1242  
1243 Periods in field names will be replaced by underscores, so that the field
1244 'addresses.city' will use the 'validate_addresses_city' method for validation.
1245  
1246 =head2 apply actions
1247  
1248 Use Type::Tiny types;
1249  
1250 use Types::Standard ('PosInteger');
1251 has_field 'foo' => ( apply => [ PosInteger ] );
1252  
1253 =head2 validate
1254  
1255 This field method can be used in addition to or instead of 'apply' actions
1256 in custom field classes.
1257 It should validate the field data and set error messages on
1258 errors with C<< $field->add_error >>.
1259  
1260 sub validate {
1261 my $field = shift;
1262 my $value = $field->value;
1263 return $field->add_error( ... ) if ( ... );
1264 }
1265  
1266 =head1 AUTHOR
1267  
1268 Gerda Shank
1269  
1270 =head1 COPYRIGHT AND LICENSE
1271  
1272 This software is copyright (c) 2017 by Gerda Shank.
1273  
1274 This is free software; you can redistribute it and/or modify it under
1275 the same terms as the Perl 5 programming language system itself.
1276  
1277 =cut
« MuForm/Localizer.pm ↑ Summary MuForm/Fields.pm »