| 1 | package Valiant::Validator::Acceptance; | ||||||||||||||||||||||||||||
| 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 accept => (is=>'ro', required=>1, default=>sub { ['1', 1, 'true', 'yes'] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | has accepted => (is=>'ro', required=>1, default=>sub {_t 'accepted'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | sub normalize_shortcut { | ||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 1 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||
| 13 | 1 | ▶ | return +{} if $arg eq '1'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | sub validate_each { | ||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 8 | my ($self, $record, $attribute, $value, $opts) = @_; | |||||||||||||||||||||||||||
| 18 | 8 | my %accept = map { $_ => 1 } @{ $self->accept }; | |||||||||||||||||||||||||||
| 19 | $record->errors->add($attribute, $self->accepted, $opts) | ||||||||||||||||||||||||||||
| 20 | 8 | ▶ | unless defined($value) && $accept{$value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $accept{$value}
MC/DC100%2/2
defined $value and $accept{$value}
defined $value
$accept{$value}
Truth table100%3/3
A and B
A= defined $valueB= $accept{$value}
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | 1; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Valiant::Validator::Acceptance - Verify that a value was accepted | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | package Local::Test::Acceptance; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | use Moo; | ||||||||||||||||||||||||||||
| 34 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | has terms_of_service => (is=>'ro'); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | validates terms_of_service => ( acceptance => 1 ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | my $object = Local::Test::Acceptance->new(terms_of_service=>0); | ||||||||||||||||||||||||||||
| 41 | $object->validate; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | warn $object->errors->_dump; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | $VAR1 = { | ||||||||||||||||||||||||||||
| 46 | 'terms_of_service' => [ | ||||||||||||||||||||||||||||
| 47 | 'Terms Of Service must be accepted', | ||||||||||||||||||||||||||||
| 48 | ] | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Validates that a value is one of a set of accepted values, typically used for | ||||||||||||||||||||||||||||
| 54 | a "terms of service" checkbox or similar agreement field. The value must be | ||||||||||||||||||||||||||||
| 55 | defined and appear in the C<accept> list; an undefined value fails (use the | ||||||||||||||||||||||||||||
| 56 | shared C<allow_undef> parameter if you need C<undef> to pass). | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | The attribute is usually a virtual one that exists only to hold the submitted | ||||||||||||||||||||||||||||
| 59 | form value and is not persisted. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 CONSTRAINTS | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | This validator supports the following constraints. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =over | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =item accept | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | An arrayref of the values considered acceptance. Defaults to | ||||||||||||||||||||||||||||
| 70 | C<< ['1', 1, 'true', 'yes'] >>. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | validates terms_of_service => ( | ||||||||||||||||||||||||||||
| 73 | acceptance => { accept => ['on'] }, | ||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =item accepted | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | The error message used when the value is not accepted. Default is translation | ||||||||||||||||||||||||||||
| 79 | tag 'accepted'. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =back | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | validates attribute => ( acceptance => 1, ... ); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Which is the same as: | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | validates attribute => ( | ||||||||||||||||||||||||||||
| 92 | acceptance => +{}, | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Not a lot of saved typing but it seems to read better. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||
| 100 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | See L<Valiant> | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | See L<Valiant> | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||