| 1 | package Valiant::Validator::Confirmation; | ||||||||||||||||||||||||||||||
| 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 | 1 | ▶ | use Valiant::Util 'throw_exception', 'debug'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 1 | with 'Valiant::Validator::Each'; | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | has confirmation => (is=>'ro', required=>1, default=>sub {_t 'confirmation'}); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 1 | has suffix => (is=>'ro', required=>1, default=>'_confirmation'); | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 13 | 1 | my ($self, $args) = @_; | |||||||||||||||||||||||||||||
| 14 | 1 | my $model_class = $self->model_class; | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | foreach my $attribute (@{$self->attributes||[]}) { | ||||||||||||||||||||||||||||
|
Condition50%1/2
$self->attributes || []
MC/DC0%0/1
$self->attributes || []
$self->attributes
Truth table50%1/2
A
A= $self->attributes
| |||||||||||||||||||||||||||||||
| 16 | 1 | my $confirmation_attribute = "${attribute}${\$self->suffix}"; | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | next if $model_class->can($confirmation_attribute); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 18 | 1 | $model_class->inject_attribute($confirmation_attribute); | |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | sub normalize_shortcut { | ||||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 23 | 1 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | if($arg eq '1') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | 1 | return +{}; | |||||||||||||||||||||||||||||
| 26 | } else { | ||||||||||||||||||||||||||||||
| 27 | 0 | return +{ suffix=>$arg }; | |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub validate_each { | ||||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=7 SCAR=21.3
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 32 | 2 | my ($self, $record, $attribute, $value, $opts) = @_; | |||||||||||||||||||||||||||||
| 33 | 2 | my $confirmation_attribute = "${attribute}${\$self->suffix}"; | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | my $confirmation = $record->can($confirmation_attribute) || | ||||||||||||||||||||||||||||
|
Condition33%1/3
$record->can($confirmation_attribute) || throw_exception('MissingMethod', ('object', $record, 'method', $confirmation_attribute))
MC/DC0%0/2
$record->can($confirmation_attribute) || throw_exception('MissingMethod', ('object', $record, 'method', $confirmation_attribute))
$record->can($confirmation_attribute)
throw_exception('MissingMethod', ('object', $record, 'method', $confirmation_attribute))
Truth table33%1/3
A || B
A= $record->can($confirmation_attribute)B= throw_exception('MissingMethod', ('object', $record, 'method', $confirmation_attribute))
| |||||||||||||||||||||||||||||||
| 35 | throw_exception MissingMethod => (object=>$record, method=>$confirmation_attribute); | ||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | 2 | my $confirmation_value = $confirmation->($record); | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||
| 39 | # If confirmation is not defined then skip this validation. If the user wants to have | ||||||||||||||||||||||||||||||
| 40 | # this defined they can use the presence validator. | ||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | return unless defined($confirmation_value) || defined($value); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $confirmation_value or defined $value
MC/DC0%0/2
defined $confirmation_value or defined $value
defined $confirmation_value
defined $value
Truth table33%1/3
A or B
A= defined $confirmation_valueB= defined $value
| |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | if( ($value||'') ne ($confirmation_value||'')) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$value || ''
$confirmation_value || ''
MC/DC0%0/1
$value || ''
$value
MC/DC0%0/1
$confirmation_value || ''
$confirmation_value
Truth table50%1/2
A
A= $value
Truth table50%1/2
A
A= $confirmation_value
| |||||||||||||||||||||||||||||||
| 45 | 1 | my $human_attribute_name = $record->human_attribute_name($attribute); | |||||||||||||||||||||||||||||
| 46 | 1 | $record->errors->add($confirmation_attribute, $self->confirmation, +{%$opts, attribute=>"$human_attribute_name"}) | |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||
| 50 | 1 | 1; | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | =head1 NAME | ||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||
| 54 | Valiant::Validator::Confirmation - Checks for a 'confirming' attributes equality. | ||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | package Local::Test::Confirmation; | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | use Moo; | ||||||||||||||||||||||||||||||
| 61 | use Valiant::Validations; | ||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | has email=> (is=>'ro'); | ||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | validates email => ( confirmation => 1 ); | ||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | my $object = Local::Test::Confirmation->new( | ||||||||||||||||||||||||||||||
| 68 | email => 'AAA@example.com', | ||||||||||||||||||||||||||||||
| 69 | email_confirmation => 'ZZZ@example.com' | ||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||
| 71 | $object->validate; | ||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | warn $object->errors->_dump; | ||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | $VAR1 = { | ||||||||||||||||||||||||||||||
| 76 | 'email_confirmation' => [ | ||||||||||||||||||||||||||||||
| 77 | "Email confirmation doesn't match 'Email'", | ||||||||||||||||||||||||||||||
| 78 | ] | ||||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||
| 83 | Use this when you have two attributes which should be set to the same value | ||||||||||||||||||||||||||||||
| 84 | (for example to confirm someone entered the correct email address or changed | ||||||||||||||||||||||||||||||
| 85 | their password to the same value). | ||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | The error message (if any) will appear associated with the confirmation attribute. | ||||||||||||||||||||||||||||||
| 88 | Error message uses tag C<confirmation> and you can override that with an init arg | ||||||||||||||||||||||||||||||
| 89 | of the same name. You can also change the suffix used to identify the confirming | ||||||||||||||||||||||||||||||
| 90 | attribute with the C<suffix> init arg (default value is '_confirmation'). | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | B<NOTE:> You don't need to add the confirmation attribute, we inject it for you during | ||||||||||||||||||||||||||||||
| 93 | validation setup. | ||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||
| 97 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | validates attribute => ( confirmation => 1, ... ); | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | Which is the same as: | ||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | validates attribute => ( | ||||||||||||||||||||||||||||||
| 104 | confirmation => +{}, | ||||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | Not a lot of saved typing but it seems to read better. You can also pass a string | ||||||||||||||||||||||||||||||
| 108 | to set the suffix: | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | validates attribute => ( confirmation => '_check', ... ); | ||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | Which is the same as: | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | validates attribute => ( | ||||||||||||||||||||||||||||||
| 115 | confirmation => +{ suffix => '_check' }, | ||||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||||
| 121 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||
| 123 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||
| 127 | =head1 AUTHOR | ||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | See L<Valiant> | ||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||
| 131 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | See L<Valiant> | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||||