File Coverage

blib/lib/HTML/FormFu/Filter/CopyValue.pm
Criterion Covered Total %
statement 15 15 100.0
branch 3 6 50.0
condition 1 3 33.3
subroutine 4 4 100.0
pod 0 1 0.0
total 23 29 79.3


line stmt bran cond sub pod time code
1             package HTML::FormFu::Filter::CopyValue;
2              
3 2     2   506 use strict;
  2         3  
  2         93  
4             our $VERSION = '2.05'; # VERSION
5              
6 2     2   6 use Moose;
  2         2  
  2         13  
7 2     2   8530 use MooseX::Attribute::FormFuChained;
  2         4  
  2         296  
8             extends 'HTML::FormFu::Filter';
9              
10             with 'HTML::FormFu::Role::NestedHashUtils';
11              
12             has field => ( is => 'rw', traits => ['FormFuChained'] );
13              
14             sub filter {
15 1     1 0 1 my ( $self, $value ) = @_;
16              
17 1 50 33     4 return $value
18             if ( defined $value && length $value );
19              
20 1 50       24 my $field_name = $self->field
21             or die "Parameter 'field' is not defined.";
22              
23 1 50       3 my $parent = $self->parent
24             or die "Can't determine my parent.";
25              
26 1         4 my $field_value
27             = $self->get_nested_hash_value( $parent->form->input, $field_name, );
28              
29 1         3 return $field_value;
30             }
31              
32             __PACKAGE__->meta->make_immutable;
33              
34             1;
35              
36             __END__
37              
38             =head1 NAME
39              
40             HTML::FormFu::Filter::CopyValue - copy the value from another field
41              
42             =head1 VERSION
43              
44             version 2.05
45              
46             =head1 SYNOPSIS
47              
48             elements:
49             - type: Text
50             name: username
51             - type: Text
52             name: nickname
53             filters:
54             - type: CopyValue
55             field: username
56              
57             =head1 DESCRIPTION
58              
59             Filter copying the value of another field if the original value of this field
60             is empty.
61              
62             =head1 CAVEATS
63              
64             If the original field contains an invalid value (a value that will be
65             constrained through a constraint) that invalid value will be copied to this
66             field (the field with the CopyValue filter). So, the user has to change two
67             fields, or you can remove the invalid value in a custom constraint.
68              
69             =head1 AUTHOR
70              
71             Mario Minati, C<mario.minati@googlemail.com>
72              
73             =head1 LICENSE
74              
75             This library is free software, you can redistribute it and/or modify it under
76             the same terms as Perl itself.
77              
78             =cut