| 1 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Constraint::DependOn; | ||||||||||||||||||||||||||||
| 4 | 6 | $HTML::FormFu::Constraint::DependOn::VERSION = '2.06'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: Multi-field Dependency Constraint | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | extends 'HTML::FormFu::Constraint'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | with 'HTML::FormFu::Role::Constraint::Others'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | use HTML::FormFu::Util qw( | |||||||||||||||||||||||||||
| 13 | DEBUG_CONSTRAINTS | ||||||||||||||||||||||||||||
| 14 | debug | ||||||||||||||||||||||||||||
| 15 | 6 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub process { | ||||||||||||||||||||||||||||
| 18 | 10 | ▶ | my ( $self, $params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=15 SCAR=35.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # check when condition | ||||||||||||||||||||||||||||
| 21 | 10 | ▶ | if ( !$self->_process_when($params) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | DEBUG_CONSTRAINTS && debug('fail when() check - skipping constraint'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | return; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 10 | my $others = $self->others; | |||||||||||||||||||||||||||
| 27 | 10 | ▶ | if ( !defined $others ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | DEBUG_CONSTRAINTS && debug('no others() - skipping constraint'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | return; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 10 | ▶ | my @names = ref $others ? @{$others} : ($others); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 10 | my @failed; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 10 | my $value = $self->get_nested_hash_value( $params, $self->nested_name ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 10 | ▶ | if ( !$self->constrain_value($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | DEBUG_CONSTRAINTS && debug('no value - skipping constraint'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 10 | for my $name (@names) { | |||||||||||||||||||||||||||
| 43 | 16 | my $value = $self->get_nested_hash_value( $params, $name ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 16 | my $ok = 0; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 16 | ▶ | if ( ref $value eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | my @err = eval { $self->constrain_values( $value, $params ) }; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | $ok = 1 if !@err && !$@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@err or $@
MC/DC0%0/2
@err or $@
@err
$@
Truth table0%0/3
A or B
A= @errB= $@
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||
| 52 | 16 | $ok = eval { $self->constrain_value($value) }; | |||||||||||||||||||||||||||
| 53 | 16 | ▶ | $ok = 0 if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 16 | ▶ | if ( !$ok ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 5 | push @failed, $name; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 10 | ▶ | return $self->mk_errors( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | { pass => @failed ? 0 : 1, | ||||||||||||||||||||||||||||
| 63 | failed => \@failed, | ||||||||||||||||||||||||||||
| 64 | names => [ $self->nested_name, @names ], | ||||||||||||||||||||||||||||
| 65 | } ); | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub constrain_value { | ||||||||||||||||||||||||||||
| 69 | 26 | ▶ | my ( $self, $value ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
constrain_value: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 26 | ▶ | return 0 if !defined $value || $value eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $value or $value eq ""
MC/DC50%1/2
not defined $value or $value eq ""
not defined $value
$value eq ""
Truth table66%2/3
not(A) or B
A= not defined $valueB= $value eq ""
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 21 | return 1; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub _localize_args { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_localize_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | return $self->parent->label; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | 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.06 | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||