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