1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
# ABSTRACT: Multiples Directive for Validation Class Field Definitions
package Validation::Class::Directive::Multiples;
use strict;
use warnings;
use base 'Validation::Class::Directive';
use Validation::Class::Util;
our $VERSION = '7.900057'; # VERSION
has 'mixin' => 0;
has 'field' => 1;
has 'multi' => 0;
has 'message' => '%s does not support multiple values';
# ensure most core directives execute before this one
has 'dependencies' => sub {{
normalization => [],
validation => [qw(
alias
between
depends_on
error
errors
filtering
filters
label
length
matches
max_alpha
max_digits
max_length
max_sum
min_alpha
min_digits
min_length
min_sum
mixin
mixin_field
name
options
pattern
readonly
required
toggle
)]
}};
sub after_validation {
my $self = shift;
my ($proto, $field, $param) = @_;
if (defined $field->{multiples} && defined $param) {
$self->after_validation_delete_clones($proto, $field, $param);
}
return $self;
}
sub after_validation_delete_clones {
my $self = shift;
my ($proto, $field, $param) = @_;
my $name = $field->name;
# this will add additional processing overhead which we hate, but is how we
# will currently prevent the reaping of strangely named fields that appear
# to be clones/clonable but are not in-fact ... so we'll check if the field
# is in the clones array
return unless grep { defined $_ and $name eq $_ }
@{$proto->stash->{'directive.validation.clones'}}
;
my ($key, $index) = $name =~ /^(.*)\:(\d+)$/;
if ($key && defined $index) {
my $value = $proto->params->delete($name);
$proto->params->{$key} ||= [];
$proto->params->{$key}->[$index] = $value;
# inherit errors from clone
if ($proto->fields->has($key) && $proto->fields->has($name)) {
$proto->fields->get($key)->errors->add(
$proto->fields->get($name)->errors->list
);
}
# remove clone permenantly
$proto->fields->delete($name);
delete $proto->stash->{'directive.validation.clones'}->[$index];
}
return $self;
}
sub before_validation {
my $self = shift;
my ($proto, $field, $param) = @_;
if (defined $field->{multiples} && defined $param) {
$self->before_validation_create_clones($proto, $field, $param);
}
return $self;
}
sub before_validation_create_clones {
my $self = shift;
my ($proto, $field, $param) = @_;
# clone fields to handle parameters with multi-values
if (isa_arrayref($param)) {
# is cloning allowed? .. in the U.S it is currently illegal :}
return $self->error(@_) if ! $field->{multiples};
# clone deterministically
my $name = $field->name;
for (my $i=0; $i < @{$param}; $i++) {
my $clone = "$name:$i";
$proto->params->add($clone => $param->[$i]);
my $label = ($field->label || $name);
my $options = {label => "$label #".($i+1), multiples => 0};
$proto->clone_field($name, $clone => $options);
# add clones to field list to be validated
push @{$proto->stash->{'validation.fields'}}, $clone
if grep { $_ eq $name } @{$proto->stash->{'validation.fields'}}
;
# record clones (to be reaped later)
push @{$proto->stash->{'directive.validation.clones'}}, $clone;
}
$proto->params->delete($name);
# remove the field the clones are based on from the fields list
@{$proto->stash->{'validation.fields'}} =
grep { $_ ne $name } @{$proto->stash->{'validation.fields'}}
if @{$proto->stash->{'validation.fields'}}
;
}
return $self;
}
sub normalize {
my $self = shift;
my ($proto, $field, $param) = @_;
# set a default value for the multiples directives
# ... the default policy is deny,allow
$field->{multiples} = 0 if ! defined $field->{multiples};
return $self;
}
1;
__END__
=pod
=head1 NAME
Validation::Class::Directive::Multiples - Multiples Directive for Validation Class Field Definitions
=head1 VERSION
version 7.900057
=head1 SYNOPSIS
use Validation::Class::Simple;
my $rules = Validation::Class::Simple->new(
fields => {
user_options => {
multiples => 1
}
}
);
# set parameters to be validated
$rules->params->add($parameters);
# validate
unless ($rules->validate) {
# handle the failures
}
=head1 DESCRIPTION
Validation::Class::Directive::Multiples is a core validation class field
directive that validates whether the associated parameters may contain a
multi-value (an array of strings).
=head1 AUTHOR
Al Newkirk <anewkirk@ana.io>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Al Newkirk.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|