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