File Coverage

blib/lib/HTML/FormFu/Constraint/DependOn.pm
Criterion Covered Total %
statement 31 42 73.8
branch 13 26 50.0
condition 2 6 33.3
subroutine 5 6 83.3
pod 0 2 0.0
total 51 82 62.2


line stmt bran cond sub pod time code
1 6     6   882 use strict;
  6         14  
  6         405  
2              
3             package HTML::FormFu::Constraint::DependOn;
4             $HTML::FormFu::Constraint::DependOn::VERSION = '2.07';
5             # ABSTRACT: Multi-field Dependency Constraint
6              
7 6     6   40 use Moose;
  6         13  
  6         51  
8             extends 'HTML::FormFu::Constraint';
9              
10             with 'HTML::FormFu::Role::Constraint::Others';
11              
12 6         3046 use HTML::FormFu::Util qw(
13             DEBUG_CONSTRAINTS
14             debug
15 6     6   41916 );
  6         15  
16              
17             sub process {
18 10     10 0 28 my ( $self, $params ) = @_;
19              
20             # check when condition
21 10 50       69 if ( !$self->_process_when($params) ) {
22 0 0       0 DEBUG_CONSTRAINTS && debug('fail when() check - skipping constraint');
23 0         0 return;
24             }
25              
26 10         328 my $others = $self->others;
27 10 50       31 if ( !defined $others ) {
28 0 0       0 DEBUG_CONSTRAINTS && debug('no others() - skipping constraint');
29 0         0 return;
30             }
31              
32 10 100       46 my @names = ref $others ? @{$others} : ($others);
  6         20  
33 10         26 my @failed;
34              
35 10         73 my $value = $self->get_nested_hash_value( $params, $self->nested_name );
36              
37 10 50       40 if ( !$self->constrain_value($value) ) {
38 0 0       0 DEBUG_CONSTRAINTS && debug('no value - skipping constraint');
39 0         0 return;
40             }
41              
42 10         29 for my $name (@names) {
43 16         58 my $value = $self->get_nested_hash_value( $params, $name );
44              
45 16         31 my $ok = 0;
46              
47 16 50       49 if ( ref $value eq 'ARRAY' ) {
48 0         0 my @err = eval { $self->constrain_values( $value, $params ) };
  0         0  
49 0 0 0     0 $ok = 1 if !@err && !$@;
50             }
51             else {
52 16         35 $ok = eval { $self->constrain_value($value) };
  16         41  
53 16 50       77 $ok = 0 if $@;
54             }
55              
56 16 100       53 if ( !$ok ) {
57 5         18 push @failed, $name;
58             }
59             }
60              
61 10 100       74 return $self->mk_errors(
62             { pass => @failed ? 0 : 1,
63             failed => \@failed,
64             names => [ $self->nested_name, @names ],
65             } );
66             }
67              
68             sub constrain_value {
69 26     26 0 56 my ( $self, $value ) = @_;
70              
71 26 100 66     136 return 0 if !defined $value || $value eq '';
72              
73 21         78 return 1;
74             }
75              
76             sub _localize_args {
77 0     0     my ($self) = @_;
78              
79 0           return $self->parent->label;
80             }
81              
82             __PACKAGE__->meta->make_immutable;
83              
84             1;
85              
86             __END__
87              
88             =pod
89              
90             =encoding UTF-8
91              
92             =head1 NAME
93              
94             HTML::FormFu::Constraint::DependOn - Multi-field Dependency Constraint
95              
96             =head1 VERSION
97              
98             version 2.07
99              
100             =head1 SYNOPSIS
101              
102             type: DependOn
103             name: foo
104             others: bar
105              
106             =head1 DESCRIPTION
107              
108             If a value is submitted for the field this constraint is attached to, then a
109             value must also be submitted for all fields named in
110             L<HTML::FormFu::Role::Constraint::Others/others>.
111              
112             By default, if any of the named fields in
113             L<HTML::FormFu::Role::Constraint::Others/others> are missing, an error will be
114             attached to each missing field. This behaviour can be changed by setting
115             any of L<HTML::FormFu::Role::Constraint::Others/attach_errors_to_base>,
116             L<HTML::FormFu::Role::Constraint::Others/attach_errors_to_others> or
117             L<HTML::FormFu::Role::Constraint::Others/attach_errors_to>.
118              
119             This constraint doesn't honour the C<not()> value.
120              
121             =head1 SEE ALSO
122              
123             Is a sub-class of, and inherits methods from
124             L<HTML::FormFu::Role::Constraint::Others>, L<HTML::FormFu::Constraint>
125              
126             L<HTML::FormFu>
127              
128             =head1 AUTHOR
129              
130             Carl Franks C<cfranks@cpan.org>
131              
132             =head1 LICENSE
133              
134             This library is free software, you can redistribute it and/or modify it under
135             the same terms as Perl itself.
136              
137             =head1 AUTHOR
138              
139             Carl Franks <cpan@fireartist.com>
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is copyright (c) 2018 by Carl Franks.
144              
145             This is free software; you can redistribute it and/or modify it under
146             the same terms as the Perl 5 programming language system itself.
147              
148             =cut