line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Controller::Validation::DFV; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Catalyst::Controller::Validation::DFV::VERSION = '0.0.8'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
{ |
6
|
|
|
|
|
|
|
$Catalyst::Controller::Validation::DFV::DIST = 'Catalyst-Controller-Validation-DFV'; |
7
|
|
|
|
|
|
|
} |
8
|
1
|
|
|
1
|
|
595351
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
45
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use base 'Catalyst::Controller'; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
726
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
7
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
84
|
|
14
|
1
|
|
|
1
|
|
1268
|
use Data::FormValidator '4.50'; |
|
1
|
|
|
|
|
44195
|
|
|
1
|
|
|
|
|
148
|
|
15
|
1
|
|
|
1
|
|
12
|
use Data::FormValidator::Constraints qw(:closures); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub form_check :Private { |
18
|
0
|
|
|
0
|
|
|
my ($self, $c, $dfv_profile) = @_; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
my $results = Data::FormValidator->check( |
21
|
|
|
|
|
|
|
$c->request->body_parameters, |
22
|
|
|
|
|
|
|
$dfv_profile |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# return our findings ... |
26
|
0
|
|
|
|
|
|
$c->stash->{validation} = $results; |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
return; |
29
|
1
|
|
|
1
|
|
1701
|
} |
|
1
|
|
|
|
|
1805
|
|
|
1
|
|
|
|
|
7
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub add_form_invalid :Private { |
32
|
|
|
|
|
|
|
my ($self, $c, $invalid_key, $invalid_value) = @_; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# if we haven't checked hte form yet, we can't add to the results |
35
|
|
|
|
|
|
|
if (not defined $c->stash->{validation}) { |
36
|
|
|
|
|
|
|
carp('form must be validated first'); |
37
|
|
|
|
|
|
|
return; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# the invalids are a keyed list of constraint names |
41
|
|
|
|
|
|
|
push |
42
|
|
|
|
|
|
|
@{ $c->stash->{validation}{invalid}{$invalid_key} }, |
43
|
|
|
|
|
|
|
$invalid_value |
44
|
|
|
|
|
|
|
; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
return; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub validation_errors_to_html :Private { |
50
|
|
|
|
|
|
|
my ($self, $c) = @_; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# factored out of a block of code I regularly paste into Controller/Root.pm |
54
|
|
|
|
|
|
|
sub refill_form :Private { |
55
|
|
|
|
|
|
|
my ($self, $c) = @_; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
if (not $c->can('fillform')) { |
58
|
|
|
|
|
|
|
# put a warning in the logs |
59
|
|
|
|
|
|
|
$c->log->warn( |
60
|
|
|
|
|
|
|
q{The context object doesn't have a fillform() method. Add 'FillInForm' to your plug-in list.} |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return; # no point in continuing |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# use Catalyst::Plugin::FillInForm to refill form data |
67
|
|
|
|
|
|
|
# in order of priority we have: |
68
|
|
|
|
|
|
|
# - stash->{formdata} |
69
|
|
|
|
|
|
|
# - ->parameters() |
70
|
|
|
|
|
|
|
# - <input value="..."> |
71
|
|
|
|
|
|
|
$c->fillform( |
72
|
|
|
|
|
|
|
{ |
73
|
|
|
|
|
|
|
# combine two hashrefs so we only make one method call |
74
|
|
|
|
|
|
|
%{ $c->request->parameters || {} }, |
75
|
|
|
|
|
|
|
%{ $c->stash->{formdata} || {} }, |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
return; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
1; |
83
|
|
|
|
|
|
|
# ABSTRACT: Form validation and refilling |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=pod |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 NAME |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Catalyst::Controller::Validation::DFV - Form validation and refilling |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 VERSION |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
version 0.0.8 |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 SYNOPSIS |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 Form Validation |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
use base 'Catalyst::Controller::Validation::DFV'; |
100
|
|
|
|
|
|
|
use Data::FormValidator::Constraints qw(:closures); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# define a DFV profile |
103
|
|
|
|
|
|
|
my $dfv_profile = { |
104
|
|
|
|
|
|
|
required => [qw< |
105
|
|
|
|
|
|
|
email_address |
106
|
|
|
|
|
|
|
phone_home |
107
|
|
|
|
|
|
|
phone_mobile |
108
|
|
|
|
|
|
|
>], |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
constraint_methods => { |
111
|
|
|
|
|
|
|
email_address => email(), |
112
|
|
|
|
|
|
|
phone_home => american_phone(), |
113
|
|
|
|
|
|
|
phone_mobile => american_phone(), |
114
|
|
|
|
|
|
|
}, |
115
|
|
|
|
|
|
|
}; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# check the form for errors |
118
|
|
|
|
|
|
|
$c->forward('form_check', [$dfv_profile]); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# perform custom/complex checking and |
121
|
|
|
|
|
|
|
# add to form validation failures |
122
|
|
|
|
|
|
|
if (not is_complex_test_ok()) { |
123
|
|
|
|
|
|
|
$c->forward( |
124
|
|
|
|
|
|
|
'add_form_invalid', |
125
|
|
|
|
|
|
|
[ $error_key, $error_constraint_name ] |
126
|
|
|
|
|
|
|
); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 Form Refilling |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
package MyApp::Controller::Root; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# ... |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
use base 'Catalyst::Controller::Validation::DFV'; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# ... |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub render : ActionClass('RenderView') { |
140
|
|
|
|
|
|
|
# ... |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub end : Private { |
144
|
|
|
|
|
|
|
my ($self, $c) = @_; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# render the page |
147
|
|
|
|
|
|
|
$c->forward('render'); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# fill in any forms |
150
|
|
|
|
|
|
|
$c->forward('refill_form'); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 DESCRIPTION |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Form-validation using a Catalyst controller and Data::FormValidator |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 EXAMPLES |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
There are L<Template::Toolkit> file examples in the examples/ directory of |
160
|
|
|
|
|
|
|
this distribution. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 AUTHOR |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Chisel <chisel@chizography.net> |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Chisel Wright. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
171
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
__END__ |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# vim: ts=8 sts=4 et sw=4 sr sta |