line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: Base Class for Validation Class Directives |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Validation::Class::Directive; |
4
|
|
|
|
|
|
|
|
5
|
109
|
|
|
109
|
|
874
|
use strict; |
|
109
|
|
|
|
|
284
|
|
|
109
|
|
|
|
|
3088
|
|
6
|
109
|
|
|
109
|
|
638
|
use warnings; |
|
109
|
|
|
|
|
279
|
|
|
109
|
|
|
|
|
2641
|
|
7
|
|
|
|
|
|
|
|
8
|
109
|
|
|
109
|
|
606
|
use Validation::Class::Util; |
|
109
|
|
|
|
|
270
|
|
|
109
|
|
|
|
|
798
|
|
9
|
|
|
|
|
|
|
|
10
|
109
|
|
|
109
|
|
752
|
use Carp 'confess'; |
|
109
|
|
|
|
|
324
|
|
|
109
|
|
|
|
|
78510
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '7.900059'; # VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# defaults |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has 'mixin' => 0; |
18
|
|
|
|
|
|
|
has 'field' => 0; |
19
|
|
|
|
|
|
|
has 'multi' => 0; |
20
|
|
|
|
|
|
|
has 'message' => '%s could not be validated'; |
21
|
|
|
|
|
|
|
has 'validator' => sub { sub{1} }; |
22
|
|
|
|
|
|
|
has 'dependencies' => sub {{ normalization => [], validation => [] }}; |
23
|
|
|
|
|
|
|
has 'name' => sub { |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my ($self) = @_; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $name = ref $self || $self; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $regexp = qr/Validation::Class::Directive::(.*)$/; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$name = $1 if $name =~ $regexp; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$name =~ s/([a-z])([A-Z])/$1_$2/g; |
34
|
|
|
|
|
|
|
$name =~ s/\W/_/g; |
35
|
|
|
|
|
|
|
$name = lc $name; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
return $name; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub new { |
42
|
|
|
|
|
|
|
|
43
|
5017
|
|
|
5017
|
0
|
9733
|
my $class = shift; |
44
|
|
|
|
|
|
|
|
45
|
5017
|
|
|
|
|
12630
|
my $arguments = $class->build_args(@_); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
confess |
48
|
|
|
|
|
|
|
"Error creating directive without a name, specifying a name is " . |
49
|
|
|
|
|
|
|
"required to instatiate a new non-subclass directive" |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
if 'Validation::Class::Directive' eq $class && ! $arguments->{name} |
52
|
|
|
|
|
|
|
|
53
|
5017
|
50
|
66
|
|
|
11198
|
; |
54
|
|
|
|
|
|
|
|
55
|
5017
|
|
|
|
|
10296
|
my $self = bless {}, $class; |
56
|
|
|
|
|
|
|
|
57
|
5017
|
|
|
|
|
7203
|
while (my($key, $value) = each %{$arguments}) { |
|
5023
|
|
|
|
|
13237
|
|
58
|
6
|
|
|
|
|
22
|
$self->$key($value); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
5017
|
|
|
|
|
15681
|
return $self; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub error { |
66
|
|
|
|
|
|
|
|
67
|
174
|
|
|
174
|
0
|
53158
|
my ($self, $proto, $field, $param, @tokens) = @_; |
68
|
|
|
|
|
|
|
|
69
|
174
|
|
66
|
|
|
901
|
my $name = $field->label || $field->name; |
70
|
|
|
|
|
|
|
|
71
|
174
|
|
|
|
|
616
|
unshift @tokens, $name; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# use custom field-level error message |
74
|
174
|
100
|
33
|
|
|
821
|
if ($field->error) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
75
|
23
|
|
|
|
|
74
|
$field->errors->add($field->error); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# use field-level error message override |
79
|
|
|
|
|
|
|
elsif (defined $field->{messages} && $field->{messages}->{$self->name}) { |
80
|
0
|
|
|
|
|
0
|
my $message = $field->{messages}->{$self->name}; |
81
|
0
|
|
|
|
|
0
|
$field->errors->add(sprintf($message, @tokens)); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# use class-level error message override |
85
|
|
|
|
|
|
|
elsif ($proto->messages->has($self->name)) { |
86
|
0
|
|
|
|
|
0
|
my $message = $proto->messages->get($self->name); |
87
|
0
|
|
|
|
|
0
|
$field->errors->add(sprintf($message, @tokens)); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# use directive error message |
91
|
|
|
|
|
|
|
else { |
92
|
151
|
|
|
|
|
600
|
$field->errors->add(sprintf($self->message, @tokens)); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
174
|
|
|
|
|
794
|
return $self; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub validate { |
100
|
|
|
|
|
|
|
|
101
|
4454
|
|
|
4454
|
0
|
7121
|
my $self = shift; |
102
|
|
|
|
|
|
|
|
103
|
4454
|
|
|
|
|
7976
|
my ($proto, $field, $param) = @_; |
104
|
|
|
|
|
|
|
|
105
|
4454
|
|
|
|
|
9648
|
my $context = $proto->stash->{'validation.context'}; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# nasty hack, we need a better way !!! |
108
|
4454
|
|
|
|
|
10124
|
$self->validator->($context, $field, $proto->params); |
109
|
|
|
|
|
|
|
|
110
|
4454
|
|
|
|
|
10594
|
return $self; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
__END__ |