lib/HTML/FormFu/Element/Repeatable.pm

filter: Statementstmt 100% 167 / 167 Branchbran 80.4% 37 / 46 Conditioncond 64.2% 9 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 17 / 17 Podpod 40.0% 2 / 5 totaltotal 91.0% 235 / 258 SCAR 36.5
CC
38
Cov
94%
_repeat_containing_block
10.0
process
9.0
_repeat_child_elements
9.0
CRAP 38.3
« lib/HTML/FormFu/ObjectUtil.pm ↑ Summary lib/HTML/FormFu/Role/Element/Input.pm »
1 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Element::Repeatable;
4 35 $HTML::FormFu::Element::Repeatable::VERSION = '2.06';
5 # ABSTRACT: repeatable block element
6  
7 35 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 extends 'HTML::FormFu::Element::Block';
10  
11 35 ▶use HTML::FormFu::Util qw( DEBUG_PROCESS debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 35 has counter_name => ( is => 'rw', traits => ['Chained'] );
16  
17 35 has _original_elements => ( is => 'rw' );
18  
19 35 has increment_field_names => (
20 is => 'rw',
21 default => 1,
22 lazy => 1,
23 traits => ['Chained'],
24 );
25  
26 # This attribute is currently not documented as FF::Model::HashRef
27 # only supports '_'
28  
29 35 has repeatable_delimiter => (
30 is => 'rw',
31 default => '_',
32 lazy => 1,
33 traits => ['Chained'],
34 );
35  
36 after BUILD => sub {
37 37 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39 37 $self->filename('repeatable');
40 37 $self->is_repeatable(1);
41  
42 37 return;
43 35 };
44  
45 sub repeat {
46 47 ▶ my ( $self, $count ) = @_;
Subroutine repeat: called CC=7 SCAR=19.6
Pod documented
47  
48 47 ▶ croak "invalid number to repeat"
Branch: 1 branch
BranchTrueFalseTotal
unless $count =~ /^[0-9]+\z/ 0 47 47
49 if $count !~ /^[0-9]+\z/;
50  
51 47 my $children;
52  
53 47 ▶ if ( $self->_original_elements ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_original_elements) { } 6 41 47
54  
55 # repeat() has already been called
56 6 $children = $self->_original_elements;
57 }
58 else {
59 41 $children = $self->_elements;
60  
61 41 $self->_original_elements($children);
62 }
63  
64 47 ▶ croak "no child elements to repeat"
Branch: 1 branch
BranchTrueFalseTotal
unless @$children 0 47 47
65 if !@$children;
66  
67 47 $self->_elements( [] );
68  
69 47 ▶ return [] if !$count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 1 46 47
70  
71 # switch behaviour
72 # If nested_name is set, we add the repeatable counter to the name
73 # of the containing block (this repeatable block).
74 # This behaviour eases the creation of client side javascript code
75 # to add and remove repeatable elements client side.
76 # If nested_name is *not* set, we add the repeatable counter to the names
77 # of the child elements (leaves of the element tree).
78 46 my $nested_name = $self->nested_name;
79 46 ▶ if ( defined $nested_name && length $nested_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nested_name and length $nested_name) { } 19 27 46
Condition66%2/3
defined $nested_name and length $nested_name
!ll&&!rl&&r
27019
MC/DC50%1/2
defined $nested_name and length $nested_name
defined $nested_name length $nested_name
Truth table66%2/3
A and B
A= defined $nested_nameB= length $nested_name
ABresult
0X0
100
111
80 19 return $self->_repeat_containing_block($count);
81 }
82 else {
83 27 return $self->_repeat_child_elements($count);
84 }
85 }
86  
87 sub _repeat_containing_block {
88 19 ▶ my ( $self, $count ) = @_;
Subroutine _repeat_containing_block: called CC=10 SCAR=23.0
89  
90 19 my $children = $self->_original_elements;
91  
92 # We must not get 'nested.nested_1' instead of 'nested_1' through the
93 # nested_name attribute of the Repeatable element, thus we extended
94 # FF::Elements::_Field nested_names method to ignore Repeatable elements.
95 19 my $nested_name = $self->nested_name;
96 19 $self->original_nested_name($nested_name);
97  
98 # delimiter between nested_name and the incremented counter
99 19 my $delimiter = $self->repeatable_delimiter;
100  
101 19 my @return;
102  
103 19 for my $rep ( 1 .. $count ) {
104  
105 # create clones of elements and put them in a new block
106 36 my @clones = map { $_->clone } @$children;
107 36 my $block = $self->element('Block');
108  
109 # initiate new block with properties of this repeatable
110 36 $block->_elements( \@clones );
111 36 $block->attributes( $self->attributes );
112 36 $block->tag( $self->tag );
113  
114 36 $block->repeatable_count($rep);
115  
116 36 ▶ if ( $self->increment_field_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->increment_field_names) 36 0 36
117  
118 # store the original nested_name attribute for later usage when
119 # building the original nested name
120 36 ▶ $block->original_nested_name( $block->nested_name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block->original_nested_name 36 0 36
121 if !defined $block->original_nested_name;
122  
123 # create new nested name with repeat counter
124 36 $block->nested_name( $nested_name . $delimiter . $rep );
125  
126 36 for my $field ( @{ $block->get_fields } ) {
127  
128 78 ▶ if ( defined( my $name = $field->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = $field->name)) 78 0 78
129  
130 # store original name for later usage when
131 # replacing the field names in constraints
132 78 ▶ $field->original_name($name)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->original_name 71 7 78
133 if !defined $field->original_name;
134  
135 # store original nested name for later usage when
136 # replacing the field names in constraints
137 78 ▶ $field->original_nested_name(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->original_nested_name 71 7 78
138 $field->build_original_nested_name )
139 if !defined $field->original_nested_name;
140 }
141 }
142 }
143  
144 36 _reparent_children($block);
145  
146 36 my @fields = @{ $block->get_fields };
147  
148 36 for my $field (@fields) {
149 19 map { $_->parent($field) }
150 78 @{ $field->_deflators },
151 78 @{ $field->_filters },
152 78 @{ $field->_constraints },
153 78 @{ $field->_inflators },
154 78 @{ $field->_validators },
155 78 @{ $field->_transformers },
156 78 @{ $field->_plugins },
157 ;
158 }
159  
160 36 for my $field (@fields) {
161 13 map { $_->repeatable_repeat( $self, $block ) }
162 78 @{ $field->_constraints };
163 }
164  
165 36 push @return, $block;
166 }
167  
168 19 return \@return;
169 }
170  
171 sub get_field_with_original_name {
172 11 ▶ my ( $self, $name, $fields ) = @_;
Subroutine get_field_with_original_name: called CC=2 SCAR=6.9
Pod undocumented
173  
174 23 ▶ my $field = first { $_->original_nested_name eq $name }
Subroutine __ANON__: called CC=1 SCAR=0.0
175 11 grep { defined $_->original_nested_name } @$fields;
176  
177 4 ▶ $field ||= first { $_->original_name eq $name }
Subroutine __ANON__: called CC=1 SCAR=0.0
178 11 ▶ grep { defined $_->original_name } @$fields;
Condition66%2/3
$field ||= &first(sub {
l!l&&r!l&&!r
920
MC/DC0%0/2
$field ||= &first(sub { $_->original_name eq $name; } , grep({defined $_->original_name;} @$fields))
$field &first(sub { $_->original_name eq $name; } , grep({defined $_->original_name;} @$fields))
Truth table66%2/3
A ||= B
A= $fieldB= &first(sub { $_->original_name eq $name; } , grep({defined $_->original_name;} @$fields))
ABresult
1X1
011
000
179  
180 11 return $field;
181 }
182  
183 sub _repeat_child_elements {
184 27 ▶ my ( $self, $count ) = @_;
Subroutine _repeat_child_elements: called CC=9 SCAR=22.0
185  
186 27 my $children = $self->_original_elements;
187  
188 # delimiter between nested_name and the incremented counter
189 27 my $delimiter = $self->repeatable_delimiter;
190  
191 27 my @return;
192  
193 27 for my $rep ( 1 .. $count ) {
194 51 my @clones = map { $_->clone } @$children;
195 51 my $block = $self->element('Block');
196  
197 51 $block->_elements( \@clones );
198 51 $block->attributes( $self->attributes );
199 51 $block->tag( $self->tag );
200  
201 51 $block->repeatable_count($rep);
202  
203 51 ▶ if ( $self->increment_field_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->increment_field_names) 42 9 51
204 42 for my $field ( @{ $block->get_fields } ) {
205  
206 83 ▶ if ( defined( my $name = $field->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = $field->name)) 83 0 83
207 83 ▶ $field->original_name($name)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->original_name 71 12 83
208 if !defined $field->original_name;
209  
210 83 ▶ $field->original_nested_name( $field->nested_name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->original_nested_name 71 12 83
211 if !defined $field->original_nested_name;
212  
213 83 $field->name( ${name} . $delimiter . $rep );
214 }
215 }
216 }
217  
218 51 _reparent_children($block);
219  
220 51 my @fields = @{ $block->get_fields };
221  
222 51 for my $field (@fields) {
223 39 map { $_->parent($field) }
224 96 @{ $field->_deflators },
225 96 @{ $field->_filters },
226 96 @{ $field->_constraints },
227 96 @{ $field->_inflators },
228 96 @{ $field->_validators },
229 96 @{ $field->_transformers },
230 96 @{ $field->_plugins },
231 ;
232 }
233  
234 51 for my $field (@fields) {
235 36 map { $_->repeatable_repeat( $self, $block ) }
236 96 @{ $field->_constraints };
237 }
238  
239 51 push @return, $block;
240 }
241  
242 27 return \@return;
243 }
244  
245 sub _reparent_children {
246 277 ▶ my $self = shift;
Subroutine _reparent_children: called CC=3 SCAR=11.0
247  
248 277 ▶ return if !$self->is_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_block 165 112 277
249  
250 112 for my $child ( @{ $self->get_elements } ) {
251 190 $child->parent($self);
252  
253 190 _reparent_children($child);
254 }
255 }
256  
257 sub process {
258 54 ▶ my $self = shift;
Subroutine process: called CC=9 SCAR=22.0
Pod undocumented
259  
260 54 my $counter_name = $self->counter_name;
261 54 my $form = $self->form;
262 54 my $count = 1;
263  
264 54 ▶ if ( defined $counter_name && defined $form->query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $counter_name and defined $form->query) 44 10 54
Condition66%2/3
defined $counter_name and defined $form->query
!ll&&!rl&&r
10044
MC/DC50%1/2
defined $counter_name and defined $form->query
defined $counter_name defined $form->query
Truth table66%2/3
A and B
A= defined $counter_nameB= defined $form->query
ABresult
0X0
100
111
265  
266 # are we in a nested-repeatable?
267 44 my $parent = $self;
268  
269 44 ▶ while ( defined( $parent = $parent->parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($parent = $parent->parent)) 49 34 83
270 49 my $field
271 = $parent->get_field( { original_name => $counter_name } );
272  
273 49 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 10 39 49
274 10 $counter_name = $field->nested_name;
275 10 last;
276 }
277 }
278  
279 44 my $input = $form->query->param($counter_name);
280  
281 44 ▶ if ( defined $input && $input =~ /^[1-9][0-9]*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and $input =~ /^[1-9][0-9]*\z/) 43 1 44
Condition66%2/3
defined $input and $input =~ /^[1-9][0-9]*\z/
!ll&&!rl&&r
1043
MC/DC50%1/2
defined $input and $input =~ /^[1-9][0-9]*\z/
defined $input $input =~ /^[1-9][0-9]*\z/
Truth table66%2/3
A and B
A= defined $inputB= $input =~ /^[1-9][0-9]*\z/
ABresult
0X0
100
111
282 43 $count = $input;
283 }
284 }
285  
286 54 ▶ if ( !$self->_original_elements ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_original_elements) 20 34 54
287 20 ▶ DEBUG_PROCESS && debug("calling \$repeatable->repeat($count)");
Branch: 1 branch
BranchTrueFalseTotal
&DEBUG_PROCESS() and debug("calling \$repeatable->repeat($count)") 0 20 20
288  
289 20 $self->repeat($count);
290 }
291  
292 54 return $self->SUPER::process(@_);
293 }
294  
295 sub content {
296 29 ▶ my $self = shift;
Subroutine content: called CC=2 SCAR=7.1
Pod documented
297  
298 29 ▶ croak "Repeatable elements do not support the content() method"
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 29 29
299 if @_;
300  
301 29 return;
302 }
303  
304 sub string {
305 22 ▶ my ( $self, $args ) = @_;
Subroutine string: called CC=3 SCAR=11.1
Pod undocumented
306  
307 22 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
022
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
308  
309 my $render
310 = exists $args->{render_data}
311 ? $args->{render_data}
312 22 ▶ : $self->render_data_non_recursive;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'render_data'} ? : 0 22 22
313  
314 # block template
315  
316 22 my @divs = map { $_->render } @{ $self->get_elements };
317  
318 22 my $html = join "\n", @divs;
319  
320 22 return $html;
321 }
322  
323 35 __PACKAGE__->meta->make_immutable;
324  
325 35 1;
326  
327 __END__
328  
329 =pod
330  
331 =encoding UTF-8
332  
333 =head1 NAME
334  
335 HTML::FormFu::Element::Repeatable - repeatable block element
336  
337 =head1 VERSION
338  
339 version 2.06
340  
341 =head1 SYNOPSIS
342  
343 ---
344 elements:
345 - type: Repeatable
346 name: my_rep
347 elements:
348 - name: foo
349 - name: bar
350  
351 Calling C<< $element->repeat(2) >> would result in the following markup:
352  
353 <div>
354 <input name="my_rep.foo_1" type="text" />
355 <input name="my_rep.bar_1" type="text" />
356 </div>
357 <div>
358 <input name="my_rep.foo_2" type="text" />
359 <input name="my_rep.bar_2" type="text" />
360 </div>
361  
362 Example of constraints:
363  
364 ----
365 elements:
366 - type: Repeatable
367 name: my_rep
368 elements:
369 - name: id
370  
371 - name: foo
372 constraints:
373 - type: Required
374 when:
375 field: 'my_rep.id' # use full nested-name
376  
377 - name: bar
378 constraints:
379 - type: Equal
380 others: 'my_rep.foo' # use full nested-name
381  
382 =head1 DESCRIPTION
383  
384 Provides a way to extend a form at run-time, by copying and repeating its
385 child elements.
386  
387 The elements intended for copying must be added before L</repeat> is called.
388  
389 Although the Repeatable element inherits from
390 L<Block|HTML::FormFu::Element::Block>, it doesn't generate a block tag
391 around all the repeated elements - instead it places each repeat of the
392 elements in a new L<Block|HTML::FormFu::Element::Block> element, which
393 inherits the Repeatable's display settings, such as L</attributes> and
394 L</tag>.
395  
396 For all constraints attached to fields within a Repeatable block which use
397 either L<others|HTML::FormFu::Role::Constraint::Others/others> or
398 L<when|HTML::FormFu::Constraint/when> containing names of fields within
399 the same Repeatable block, when L<repeat> is called, those names will
400 automatically be updated to the new nested-name for each field (taking
401 into account L<increment_field_names>).
402  
403 =head1 METHODS
404  
405 =head2 repeat
406  
407 Arguments: [$count]
408  
409 Return Value: $arrayref_of_new_child_blocks
410  
411 This method creates C<$count> number of copies of the child elements.
412 If no argument C<$count> is provided, it defaults to C<1>.
413  
414 Note that C<< $form->process >> will call L</repeat> automatically to ensure the
415 initial child elements are correctly set up - unless you call L</repeat>
416 manually first, in which case the child elements you created will be left
417 untouched (otherwise L<process|HTML::FormFu/process> would overwrite your
418 changes).
419  
420 Any subsequent call to L</repeat> will delete the previously copied elements
421 before creating new copies - this means you cannot make repeated calls to
422 L</repeat> within a loop to create more copies.
423  
424 Each copy of the elements returned are contained in a new
425 L<Block|HTML::FormFu::Element::Block> element. For example, calling
426 C<< $element->repeat(2) >> on a Repeatable element containing 2 Text fields
427 would return 2 L<Block|HTML::FormFu::Element::Block> elements, each
428 containing a copy of the 2 Text fields.
429  
430 =head2 counter_name
431  
432 Arguments: $name
433  
434 If true, the L<HTML::FormFu/query> will be searched during
435 L<HTML::FormFu/process> for a parameter with the given name. The value for
436 that parameter will be passed to L</repeat>, to automatically create the
437 new copies.
438  
439 If L</increment_field_names> is true (the default), this is essential: if the
440 elements corresponding to the new fieldnames (foo_1, bar_2, etc.) are not
441 present on the form during L<HTML::FormFu/process>, no Processors
442 (Constraints, etc.) will be run on the fields, and their values will not
443 be returned by L<HTML::FormFu/params> or L<HTML::FormFu/param>.
444  
445 =head2 increment_field_names
446  
447 Arguments: $bool
448  
449 Default Value: 1
450  
451 If true, then all fields will have C<< _n >> appended to their name, where
452 C<n> is the L</repeatable_count> value.
453  
454 =head2 repeatable_count
455  
456 This is set on each new L<Block|HTML::FormFu::Element::Block> element
457 returned by L</repeat>, starting at number C<1>.
458  
459 Because this is an 'inherited accessor' available on all elements, it can be
460 used to determine whether any element is a child of a Repeatable element.
461  
462 Only available after L<repeat> has been called.
463  
464 =head2 repeatable_count_no_inherit
465  
466 A non-inheriting variant of L</repeatable_count>.
467  
468 =head2 nested_name
469  
470 If the L</nested_name> attribute is set, the naming scheme of the Repeatable
471 element's children is switched to add the counter to the repeatable blocks
472 themselves.
473  
474 ---
475 elements:
476 - type: Repeatable
477 nested_name: my_rep
478 elements:
479 - name: foo
480 - name: bar
481  
482 Calling C<< $element->repeat(2) >> would result in the following markup:
483  
484 <div>
485 <input name="my_rep_1.foo" type="text" />
486 <input name="my_rep_1.bar" type="text" />
487 </div>
488 <div>
489 <input name="my_rep_2.foo" type="text" />
490 <input name="my_rep_2.bar" type="text" />
491 </div>
492  
493 Because this is an 'inherited accessor' available on all elements, it can be
494 used to determine whether any element is a child of a Repeatable element.
495  
496 =head2 attributes
497  
498 =head2 attrs
499  
500 Any attributes set will be passed to every repeated Block of elements.
501  
502 ---
503 elements:
504 - type: Repeatable
505 name: my_rep
506 attributes:
507 class: rep
508 elements:
509 - name: foo
510  
511 Calling C<< $element->repeat(2) >> would result in the following markup:
512  
513 <div class="rep">
514 <input name="my_rep.foo_1" type="text" />
515 </div>
516 <div class="rep">
517 <input name="my_rep.foo_2" type="text" />
518 </div>
519  
520 See L<HTML::FormFu/attributes> for details.
521  
522 =head2 tag
523  
524 The L</tag> value will be passed to every repeated Block of elements.
525  
526 ---
527 elements:
528 - type: Repeatable
529 name: my_rep
530 tag: span
531 elements:
532 - name: foo
533  
534 Calling C<< $element->repeat(2) >> would result in the following markup:
535  
536 <span>
537 <input name="my_rep.foo_1" type="text" />
538 </span>
539 <span>
540 <input name="my_rep.foo_2" type="text" />
541 </span>
542  
543 See L<HTML::FormFu::Element::Block/tag> for details.
544  
545 =head2 auto_id
546  
547 As well as the usual substitutions, any instances of C<%r> will be
548 replaced with the value of L</repeatable_count>.
549  
550 See L<HTML::FormFu::Element::Block/auto_id> for further details.
551  
552 ---
553 elements:
554 - type: Repeatable
555 name: my_rep
556 auto_id: "%n_%r"
557 elements:
558 - name: foo
559  
560 Calling C<< $element->repeat(2) >> would result in the following markup:
561  
562 <div>
563 <input name="my_rep.foo_1" id="foo_1" type="text" />
564 </div>
565 <div>
566 <input name="my_rep.foo_2" id="foo_2" type="text" />
567 </div>
568  
569 =head2 content
570  
571 Not supported for Repeatable elements - will throw a fatal error if called as
572 a setter.
573  
574 =head1 CAVEATS
575  
576 =head2 Unsupported Constraints
577  
578 Note that constraints with an L<others|HTML::FormFu::Role::Constraint::Others>
579 method do not work correctly within a Repeatable block. Currently, these are:
580 L<AllOrNone|HTML::FormFu::Constraint::AllOrNone>,
581 L<DependOn|HTML::FormFu::Constraint::DependOn>,
582 L<Equal|HTML::FormFu::Constraint::Equal>,
583 L<MinMaxFields|HTML::FormFu::Constraint::MinMaxFields>,
584 L<reCAPTCHA|HTML::FormFu::Constraint::reCAPTCHA>.
585 Also, the L<CallbackOnce|HTML::FormFu::Constraint::CallbackOnce> constraint
586 won't work within a Repeatable block, as it wouldn't make much sense.
587  
588 =head2 Work-arounds
589  
590 See L<HTML::FormFu::Filter::ForceListValue> to address a problem with
591 L<increment_field_names> disabled, and increading the L<repeat> on the
592 server-side.
593  
594 =head1 SEE ALSO
595  
596 Is a sub-class of, and inherits methods from
597 L<HTML::FormFu::Element::Block>,
598 L<HTML::FormFu::Element>
599  
600 L<HTML::FormFu>
601  
602 =head1 AUTHOR
603  
604 Carl Franks, C<cfranks@cpan.org>
605  
606 =head1 LICENSE
607  
608 This library is free software, you can redistribute it and/or modify it under
609 the same terms as Perl itself.
610  
611 =head1 AUTHOR
612  
613 Carl Franks <cpan@fireartist.com>
614  
615 =head1 COPYRIGHT AND LICENSE
616  
617 This software is copyright (c) 2018 by Carl Franks.
618  
619 This is free software; you can redistribute it and/or modify it under
620 the same terms as the Perl 5 programming language system itself.
621  
622 =cut
« lib/HTML/FormFu/ObjectUtil.pm ↑ Summary lib/HTML/FormFu/Role/Element/Input.pm »