line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
4
|
|
|
4
|
|
2536
|
use strict; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
208
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package HTML::FormFu::Validator; |
4
|
|
|
|
|
|
|
$HTML::FormFu::Validator::VERSION = '2.07'; |
5
|
|
|
|
|
|
|
# ABSTRACT: Validator Base Class |
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
22
|
use Moose; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
30
|
|
8
|
|
|
|
|
|
|
extends 'HTML::FormFu::Processor'; |
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
30951
|
use HTML::FormFu::Exception::Validator; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
198
|
|
11
|
4
|
|
|
4
|
|
37
|
use Scalar::Util qw( reftype blessed ); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
300
|
|
12
|
4
|
|
|
4
|
|
26
|
use Carp qw( croak ); |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
1687
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub process { |
15
|
8
|
|
|
8
|
0
|
19
|
my ( $self, $params ) = @_; |
16
|
|
|
|
|
|
|
|
17
|
8
|
|
|
|
|
24
|
my $value = $self->get_nested_hash_value( $params, $self->nested_name ); |
18
|
|
|
|
|
|
|
|
19
|
8
|
|
|
|
|
16
|
my @errors; |
20
|
|
|
|
|
|
|
|
21
|
8
|
100
|
100
|
|
|
56
|
if ( ( reftype($value) || '' ) eq 'ARRAY' ) { |
22
|
2
|
|
|
|
|
6
|
push @errors, eval { $self->validate_values( $value, $params ) }; |
|
2
|
|
|
|
|
32
|
|
23
|
|
|
|
|
|
|
|
24
|
2
|
50
|
|
|
|
219
|
if ($@) { |
25
|
0
|
|
|
|
|
0
|
push @errors, $self->return_error($@); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
else { |
29
|
6
|
|
|
|
|
13
|
my $ok = eval { $self->validate_value( $value, $params ) }; |
|
6
|
|
|
|
|
25
|
|
30
|
|
|
|
|
|
|
|
31
|
6
|
100
|
66
|
|
|
45
|
if ( $@ or !$ok ) { |
32
|
1
|
|
|
|
|
11
|
push @errors, $self->return_error($@); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
8
|
|
|
|
|
32
|
return @errors; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub validate_values { |
40
|
2
|
|
|
2
|
0
|
8
|
my ( $self, $values, $params ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
4
|
my @errors; |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
8
|
for my $value (@$values) { |
45
|
6
|
|
|
|
|
12
|
my $ok = eval { $self->validate_value( $value, $params ) }; |
|
6
|
|
|
|
|
18
|
|
46
|
|
|
|
|
|
|
|
47
|
6
|
50
|
|
|
|
25
|
push @errors, $self->return_error($@) if !$ok; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
2
|
|
|
|
|
8
|
return @errors; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub validate_value { |
54
|
0
|
|
|
0
|
0
|
0
|
croak "validate() should be overridden"; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub return_error { |
58
|
1
|
|
|
1
|
0
|
4
|
my ( $self, $err ) = @_; |
59
|
|
|
|
|
|
|
|
60
|
1
|
50
|
33
|
|
|
10
|
if ( !blessed $err || !$err->isa('HTML::FormFu::Exception::Validator') ) { |
61
|
0
|
0
|
|
|
|
0
|
$err = HTML::FormFu::Exception::Validator->new( |
62
|
|
|
|
|
|
|
$err ? { message => $err } : () ); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
3
|
return $err; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
__END__ |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=pod |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=encoding UTF-8 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 NAME |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
HTML::FormFu::Validator - Validator Base Class |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 VERSION |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
version 2.07 |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 SYNOPSIS |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 DESCRIPTION |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 METHODS |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 CORE VALIDATORS |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=over |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item L<HTML::FormFu::Validator::Callback> |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=back |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 BEST PRACTICES |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Try to avoid using callbacks if possible. Below is a more maintainable |
103
|
|
|
|
|
|
|
and reusable approach, which also keeps the code out of the controller. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
A normal application's directory would contain: |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
lib/HTML/FormFu/Constraint/MyApp/ |
108
|
|
|
|
|
|
|
lib/HTML/FormFu/Validator/MyApp/ |
109
|
|
|
|
|
|
|
lib/HTML/FormFu/Plugin/MyApp/ |
110
|
|
|
|
|
|
|
etc. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Then, the form config file would just need: |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
validator: 'MyApp::SomeValidator' |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
And the class would be something like this: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
package HTML::FormFu::Validator::MyApp::SomeValidator; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
use Moose; |
121
|
|
|
|
|
|
|
extends 'HTML::FormFu::Validator'; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub validate_value { |
124
|
|
|
|
|
|
|
my ( $self, $value, $params ) = @_; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $c = $self->form->stash->{context}; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
return 1 if $c->model('DBIC')->is_valid($value); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# assuming you want to return a custom error message |
131
|
|
|
|
|
|
|
# which perhaps includes something retrieved from the model |
132
|
|
|
|
|
|
|
# otherwise, just return 0 |
133
|
|
|
|
|
|
|
die HTML::FormFu::Exception::Validator->new({ |
134
|
|
|
|
|
|
|
message => 'custom error message', |
135
|
|
|
|
|
|
|
}); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 AUTHOR |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Carl Franks, C<cfranks@cpan.org> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 LICENSE |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify it under |
147
|
|
|
|
|
|
|
the same terms as Perl itself. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 AUTHOR |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Carl Franks <cpan@fireartist.com> |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
This software is copyright (c) 2018 by Carl Franks. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
158
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |