File Coverage

blib/lib/HTML/FormFu/Filter/ForceListValue.pm
Criterion Covered Total %
statement 11 11 100.0
branch 2 2 100.0
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 16 17 94.1


line stmt bran cond sub pod time code
1 2     2   852 use strict;
  2         5  
  2         137  
2              
3             package HTML::FormFu::Filter::ForceListValue;
4             $HTML::FormFu::Filter::ForceListValue::VERSION = '2.07';
5             # ABSTRACT: convert a single value into a 1-item-list
6              
7 2     2   14 use Moose;
  2         5  
  2         16  
8             extends 'HTML::FormFu::Filter';
9              
10             sub process {
11 3     3 0 9 my ( $self, $result, $params ) = @_;
12              
13 3         25 my $name = $self->nested_name;
14 3         27 my $value = $self->get_nested_hash_value( $params, $name );
15              
16 3 100       15 return if 'ARRAY' eq ref $value;
17              
18 2         15 $self->set_nested_hash_value( $params, $name, [$value] );
19             }
20              
21             1;
22              
23             __END__
24              
25             =pod
26              
27             =encoding UTF-8
28              
29             =head1 NAME
30              
31             HTML::FormFu::Filter::ForceListValue - convert a single value into a 1-item-list
32              
33             =head1 VERSION
34              
35             version 2.07
36              
37             =head1 SYNOPSIS
38              
39             element:
40             - type: Repeatable
41             increment_field_names: 0
42             elements:
43             - name: foo
44             render_processed_value: 1
45             filter:
46             - ForceListValue
47              
48             =head1 DESCRIPTION
49              
50             Causes a single submitted value to be changed to a list containing 1 item.
51              
52             Solves an uncommon problem with a
53             L<Repeatable block|HTML::FormFu::Element::Repeatable> with
54             L<increment_field_names|HTML::FormFu::Element::Repeatable/increment_field_names>
55             disabled, when manually increasing the
56             L<repeat|HTML::FormFu::Element::Repeatable/repeat> count after the form was
57             submitted with only a single value for each of the Repeatable's fields.
58              
59             If these circumstances, when rendered, every repeated field would have the
60             initially-submitted value as its default.
61              
62             Using this filter, and setting
63             L<render_processed_value|HTML::FormFu/render_processed_value> to C<true> will
64             ensure that only the first repetition of each field will have the submitted
65             value as its default; all subsequent repetitions will have no default value.
66              
67             =head1 AUTHOR
68              
69             Carl Franks
70              
71             =head1 LICENSE
72              
73             This library is free software, you can redistribute it and/or modify it under
74             the same terms as Perl itself.
75              
76             =head1 AUTHOR
77              
78             Carl Franks <cpan@fireartist.com>
79              
80             =head1 COPYRIGHT AND LICENSE
81              
82             This software is copyright (c) 2018 by Carl Franks.
83              
84             This is free software; you can redistribute it and/or modify it under
85             the same terms as the Perl 5 programming language system itself.
86              
87             =cut