1# ABSTRACT: Alias Directive for Validation Class Field Definitions
2
3package Validation::Class::Directive::Alias;
4
5use strict;
6use warnings;
7
8use base 'Validation::Class::Directive';
9
10use Validation::Class::Util;
11
12our $VERSION = '7.900057'; # VERSION
13
14
15has 'mixin'        => 0;
16has 'field'        => 1;
17has 'multi'        => 0;
18has 'dependencies' => sub {{
19    normalization => ['name'],
20    validation    => ['name']
21}};
22
23sub normalize {
24
25    my ($self, $proto, $field, $param) = @_;
26
27    # create a map from aliases if applicable
28
29    $self->execute_alias_mapping($proto, $field, $param);
30
31    return $self;
32
33}
34
35sub before_validation {
36
37    my ($self, $proto, $field, $param) = @_;
38
39    # create a map from aliases if applicable
40
41    $self->execute_alias_mapping($proto, $field, $param);
42
43    return $self;
44
45}
46
47sub execute_alias_mapping {
48
49    my ($self, $proto, $field, $param) = @_;
50
51    if (defined $field->{alias}) {
52
53        my $name = $field->{name};
54
55        my $aliases = isa_arrayref($field->{alias}) ?
56            $field->{alias} : [$field->{alias}]
57        ;
58
59        foreach my $alias (@{$aliases}) {
60
61            if ($proto->params->has($alias)) {
62
63                # rename the submitted parameter alias with the field name
64                $proto->params->add($name => $proto->params->delete($alias));
65
66                push @{$proto->stash->{'validation.fields'}}, $name unless
67                    grep { $name eq $_} @{$proto->stash->{'validation.fields'}}
68                ;
69
70            }
71
72        }
73
74    }
75
76    return $self;
77
78}
79
801;
81
82__END__
83
84=pod
85
86=head1 NAME
87
88Validation::Class::Directive::Alias - Alias Directive for Validation Class Field Definitions
89
90=head1 VERSION
91
92version 7.900057
93
94=head1 SYNOPSIS
95
96    use Validation::Class::Simple;
97
98    my $rules = Validation::Class::Simple->new(
99        fields => {
100            login  => {
101                alias => 'username'
102            }
103        }
104    );
105
106    # set parameters to be validated
107    $rules->params->add($parameters);
108
109    # validate
110    unless ($rules->validate) {
111        # handle the failures
112    }
113
114=head1 DESCRIPTION
115
116Validation::Class::Directive::Alias is a core validation class field directive
117that provides the ability to map arbitrary parameter names with a field's
118parameter value.
119
120=over 8
121
122=item * alternative argument: an-array-of-aliases
123
124This directive can be passed a single value or an array of values:
125
126    fields => {
127        login  => {
128            alias => ['username', 'email_address']
129        }
130    }
131
132=back
133
134=head1 AUTHOR
135
136Al Newkirk <anewkirk@ana.io>
137
138=head1 COPYRIGHT AND LICENSE
139
140This software is copyright (c) 2011 by Al Newkirk.
141
142This is free software; you can redistribute it and/or modify it under
143the same terms as the Perl 5 programming language system itself.
144
145=cut
146