| 1 | package Valiant::Validator::OnlyOf; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Valiant::I18N; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 1 | with 'Valiant::Validator::Each'; | |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 1 | has members => (is=>'ro', required=>1); | |||||||||||||||||||||||||||||||||
| 9 | 1 | has max_allowed => (is=>'ro', required=>1, default=>1); | |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | has only_of => (is=>'ro', required=>1, default=>sub {_t 'only_of'}); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub normalize_shortcut { | ||||||||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 13 | 1 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||||||||
| 14 | 1 | return {members => $arg}; | |||||||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | sub validate_each { | ||||||||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 18 | 3 | my ($self, $record, $attribute, $value, $options) = @_; | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 3 | my @members = $self->_cb_value($record, $self->members); | |||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | @members = @{$members[0]} if (ref($members[0])||'') eq 'ARRAY'; # lets the callback be an array or arrayref | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $members[0] || ''
MC/DC0%0/1
ref $members[0] || ''
ref $members[0]
Truth table50%1/2
A
A= ref $members[0]
| |||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | my @group_values = map { | ||||||||||||||||||||||||||||||||||
| 24 | 6 | $record->read_attribute_for_validation($_); | |||||||||||||||||||||||||||||||||
| 25 | } grep { | ||||||||||||||||||||||||||||||||||
| 26 | 3 | $_ ne $attribute; # probably a common error | |||||||||||||||||||||||||||||||||
| 27 | } @members; | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 3 | push @group_values, $value; | |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | my $count_not_blank = grep { | ||||||||||||||||||||||||||||||||||
| 32 | 3 | ▶ | defined $_ && ( $_ ne '' && $_ !~m/^\s+$/) | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
defined $_ && ($_ ne '' && !($_ =~ /^\s+$/))
$_ ne "" and not $_ =~ /^\s+$/
MC/DC66%2/3
defined $_ && ($_ ne '' && !($_ =~ /^\s+$/))
defined $_
$_ ne ""
not $_ =~ /^\s+$/
Truth table75%3/4
A && B and not(C)
A= defined $_B= $_ ne ""C= not $_ =~ /^\s+$/
| |||||||||||||||||||||||||||||||||||
| 33 | } @group_values; | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 3 | my $max_allowed = $self->_cb_value($record, $self->max_allowed); | |||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | unless( $count_not_blank <= $max_allowed) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 37 | 1 | my %opts = ( | |||||||||||||||||||||||||||||||||
| 38 | %$options, | ||||||||||||||||||||||||||||||||||
| 39 | count => $max_allowed, | ||||||||||||||||||||||||||||||||||
| 40 | count_not_blank => $count_not_blank, | ||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||
| 42 | 1 | $record->errors->add($attribute, $self->only_of, \%opts); | |||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | Valiant::Validator::OnlyOf - Limit the number of fields not blank in a group | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | package Local::Test::OnlyOf; | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | use Moo; | ||||||||||||||||||||||||||||||||||
| 57 | use Valiant::Validations; | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | has opt1 => (is=>'ro'); | ||||||||||||||||||||||||||||||||||
| 60 | has opt2 => (is=>'ro'); | ||||||||||||||||||||||||||||||||||
| 61 | has opt3 => (is=>'ro'); | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | validates opt1 => ( only_of => { | ||||||||||||||||||||||||||||||||||
| 64 | members => ['opt2','opt3'], | ||||||||||||||||||||||||||||||||||
| 65 | max_allowed => 1, # This is the default value | ||||||||||||||||||||||||||||||||||
| 66 | }); | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | my $object = Local::Test::OnlyOf->new(opt1=>1, opt2=>1); | ||||||||||||||||||||||||||||||||||
| 69 | $object->validate; | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | warn $object->errors->_dump; | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | $VAR1 = { | ||||||||||||||||||||||||||||||||||
| 74 | 'opt1' => [ | ||||||||||||||||||||||||||||||||||
| 75 | 'Opt1 please choose only 1 field' | ||||||||||||||||||||||||||||||||||
| 76 | ] | ||||||||||||||||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | Limits the number of fields in a group that can be not blank. Useful when | ||||||||||||||||||||||||||||||||||
| 82 | you have a group of optional fields that you want the user to fill in only | ||||||||||||||||||||||||||||||||||
| 83 | one (or a subset of). Default is to allow only one member of the defined group | ||||||||||||||||||||||||||||||||||
| 84 | to be not blank, and you can override that with the C<max_allowed> paramters. | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | Both C<members> and C<max_allowed> can be a subref that gets the first argument | ||||||||||||||||||||||||||||||||||
| 87 | as the object and is expected to return something valid. Useful if for example | ||||||||||||||||||||||||||||||||||
| 88 | you have different rules for the group members or size based on the data. | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | Uses C<only_of> as the translation tag and you can set | ||||||||||||||||||||||||||||||||||
| 91 | that to override the message. | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | validates attribute => ( only_of => ['opt2','opt3'], ... ); | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | Which is the same as: | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | validates attribute => ( | ||||||||||||||||||||||||||||||||||
| 102 | only_of => { | ||||||||||||||||||||||||||||||||||
| 103 | members => ['opt2','opt3'], | ||||||||||||||||||||||||||||||||||
| 104 | }, | ||||||||||||||||||||||||||||||||||
| 105 | ... ); | ||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||||||||
| 110 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | See L<Valiant> | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | See L<Valiant> | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||||||||