File Coverage

lib/Egg/Plugin/FormValidator/Simple.pm
Criterion Covered Total %
statement 9 30 30.0
branch 0 12 0.0
condition 0 11 0.0
subroutine 3 6 50.0
pod 2 2 100.0
total 14 61 22.9


line stmt bran cond sub pod time code
1             package Egg::Plugin::FormValidator::Simple;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Simple.pm 261 2008-02-17 17:11:18Z lushe $
6             #
7 1     1   792 use strict;
  1         6  
  1         65  
8 1     1   10 use warnings;
  1         3  
  1         45  
9 1     1   1339 use FormValidator::Simple;
  1         78441  
  1         4  
10              
11             our $VERSION = '3.00';
12              
13             sub _setup {
14 0     0     my($e)= @_;
15 0   0       my $conf= $e->config->{plugin_validator} ||= {};
16 0 0         if (my $plugins= $conf->{plugins}) {
17 0           FormValidator::Simple->import(@$plugins);
18             }
19 0 0         if (my $messages= $conf->{messages}) {
20 0           FormValidator::Simple->set_messages($messages);
21             }
22 0 0         if (my $options= $conf->{options}) {
23 0           FormValidator::Simple->set_option(%$options);
24             }
25 0 0         if (my $format= $conf->{message_format}) {
26 0           FormValidator::Simple->set_message_format($format);
27             }
28 0           $e->next::method;
29             }
30              
31             sub form {
32 0     0 1   my $e= shift;
33 0   0       $e->{form} ||= FormValidator::Simple->new;
34 0 0         if (@_) {
35 0 0 0       my($form, $param)= ref($_[0]) eq 'ARRAY'
36             ? ($_[0], ($_[1] || $e->request)): ([@_], $e->request);
37 0           return $e->{form}->check($param, $form);
38             }
39 0           $e->{form}->results;
40             }
41             sub set_invalid_form {
42 0     0 1   my $e= shift;
43 0   0       $e->{form} ||= FormValidator::Simple->new;
44 0           $e->{form}->set_invalid(@_);
45 0           $e->{form}->results;
46             }
47              
48             1;
49              
50             __END__
51              
52             =head1 NAME
53              
54             Egg::Plugin::FormValidator::Simple - Validator for Egg with FormValidator::Simple
55              
56             =head1 SYNOPSIS
57              
58             use Catalyst qw/ FormValidator::Simple FillInForm /;
59            
60             __PACKAGE__->egg_startup(
61            
62             plugin_validator => {
63             plugins => ['CreditCard', 'Japanese'],
64             options => { charset => 'euc'},
65             },
66            
67             );
68              
69             $e->form(
70             param1 => [qw/NOT_BLANK ASCII/, [qw/LENGTH 4 10/]],
71             param2 => [qw/NOT_BLANK/, [qw/JLENGTH 4 10/]],
72             mail1 => [qw/NOT_BLANK EMAIL_LOOSE/],
73             mail2 => [qw/NOT_BLANK EMAIL_LOOSE/],
74             { mail => [qw/mail1 mail2/] } => ['DUPLICATION'],
75             );
76            
77             print $e->form->valid('param1');
78            
79             if ( some condition... ) {
80             $e->form( other_param => [qw/NOT_INT/] );
81             }
82            
83             if ( some condition... ) {
84             # set your original invalid type.
85             $e->set_invalid_form( param3 => 'MY_ERROR' );
86             }
87            
88             if ( $e->form->has_error ) {
89            
90             if ( $e->form->missing('param1') ) {
91             ...
92             }
93            
94             if ( $e->form->invalid( param1 => 'ASCII' ) ) {
95             ...
96             }
97            
98             if ( $e->form->invalid( param3 => 'MY_ERROR' ) ) {
99             ...
100             }
101            
102             }
103              
104             =head1 DESCRIPTION
105              
106             This plugin allows you to validate request parameters with FormValidator::Simple.
107             See L<FormValidator::Simple> for more information.
108              
109             This behaves like as L<Catalyst::Plugin::FormValidator>.
110              
111             =head2 METHODS
112              
113             =head2 form
114              
115             =head2 set_invalid_form
116              
117             =head1 CONFIGURATION
118              
119             set config with 'plugin_validator' key.
120              
121             __PACKAGE__->egg_startup( plugin_validator => { ... } );
122              
123             or MyApp::config.pm is edited.
124              
125             =head2 PLUGINS
126              
127             If you want to use some plugins for FormValidator::Simple, you can set like following.
128              
129             plugin_validator => {
130             plugins => [qw/Japanese CreditCard DBIC::Unique/],
131             },
132              
133             In this example, FormValidator::Simple::Plugin::Japanese, FormValidator::Simple::Plugin::CreditCard,
134             and FormValidator::Simple::Plugin::DBIC::Unique are loaded.
135              
136             =head2 OPTIONS
137              
138             When you set some options needed by specific validations, do like this.
139              
140             plugin_validator => {
141             plugins => [qw/Japanese CreditCard DBIC::Unique/],
142             options => {
143             charset => 'euc',
144             },
145             },
146              
147             =head1 VALIDATION
148              
149             use 'form' method, see L<FormValidator::Simple> in detail.
150              
151             # execute validation.
152             $e->form(
153             name => [qw/NOT_BLANK ASCII/, [qw/LENGTH 0 20/] ],
154             email => [qw/NOT_BLANK EMAIL_LOOSE/, [qw/LENGTH 0 20/] ],
155             { unique => [qw/name email/] } => [qw/DBIC_UNIQUE User name email/],
156             );
157            
158             if ( ... ) {
159            
160             # execute validation one more time in specific condition.
161             $e->form( ... );
162            
163             }
164            
165             # check result.
166             # you can pick up result-object with 'form' method
167            
168             my $result = $e->form;
169            
170             if ( $result->has_error ) {
171            
172             # this is same as
173             # if ( $result->has_missing or $result->has_invalid )
174            
175             $e->detach('add');
176            
177             }
178              
179             =head1 HANDLING SUCCESSFUL RESULT
180              
181             After it passes all validations, you may wanna put input-data into database.
182             It's a elegant way to use [ L<Class::DBI> and L<Class::DBI::FromForm> ] or [ L<DBIx::Class> and L<DBIx::Class::WebForm> ].
183              
184             $c->form(
185             name => [qw/NOT_BLANK/],
186             email => [qw/NOT_BLANK/],
187             );
188            
189             my $result = $c->form;
190             if ( $result->has_error ) {
191             ... error.
192             }
193            
194             my $user = MyProj::Model::DBIC::User->create_from_form($result);
195            
196             # this behaves like this...
197             # MyProj::Model::DBIC::User->create({
198             # name => $result->valid('name'),
199             # email => $result->valid('email'),
200             # });
201             #
202             # if the key exists as the table's column, set the value with 'valid'
203              
204             Here, I explain about 'valid' method. If the value indicated with key-name passes validations,
205             You can get the data with 'valid',
206              
207             my $result = $c->form(
208             name => [qw/NOT_BLANK/],
209             email => [qw/NOT_BLANK/],
210             );
211            
212             print $result->valid('name');
213            
214             print $result->valid('email');
215            
216             But, this is for only single key validation normally.
217              
218             my $result = $c->form(
219             name => [qw/NOT_BLANK/], # single key validation
220             { mail_dup => [qw/email email2/] } => ['DUPLICATION'] # multiple keys one
221             );
222            
223             print $result->valid('name'); # print out the value of 'name'
224            
225             print $result->valid('mail_dup'); # no value.
226              
227             There are exceptions. These are 'DATETIME', 'DATE'.
228              
229             my $result = $c->form(
230             { created_on => [qw/created_year created_month created_day/] }
231             =>
232             [qw/DATETIME/],
233             );
234            
235             print $result->valid('created_on'); #print out datetime string like "2005-11-23 00:00:00".
236              
237             If you set some class around datetime in configuration. It returns object of the class you indicate.
238             You can choose from L<Time::Piece> and L<DateTime>. For example...
239              
240             plugin_validator => {
241             validator => {
242             plugins => [...],
243             options => {
244             datetime_class => 'Time::Piece',
245             },
246             },
247             );
248              
249             or
250              
251             plugin_validator => {
252             validator => {
253             plugins => [...],
254             options => {
255             datetime_class => 'DateTime',
256             time_zone => 'Asia/Tokyo',
257             },
258             },
259             );
260              
261             then
262              
263             my $result = $c->form(
264             { created_on => [qw/created_year created_month created_day/] }
265             =>
266             [qw/DATETIME/],
267             );
268              
269             my $dt = $result->valid('created_on');
270            
271             print $dt->ymd;
272            
273             MyProj::Model::CDBI::User->create_from_form($result);
274              
275             This may be useful when you define 'has_a' relation for datetime columns.
276             For example, in your table class inherits 'Class::DBI'
277              
278             __PACKAGE__->has_a( created_on => 'DateTime',
279             inflate => ...,
280             deflate => ...,
281             );
282              
283             And see also L<Class::DBI::Plugin::TimePiece>, L<Class::DBI::Plugin::DateTime>.
284              
285             =head1 MESSAGE HANDLING
286              
287             in template file, you can handle it in detail.
288              
289             [% IF c.form.has_error %]
290             <p>Input Error</p>
291             <ul>
292             [% IF c.form.missing('name') %]
293             <li>input name!</li>
294             [% END %]
295             [% IF c.form.invalid('name') %]
296             <li>name is wrong</li>
297             [% END %]
298             [% IF c.form.invalid('name', 'ASCII') %]
299             <li>input name with ascii code.</li>
300             [% END %]
301             [% IF c.form.invalid('name', 'LENGTH') %]
302             <li>wrong length for name.</li>
303             [% END %]
304             </ul>
305             [% END %]
306              
307             or, make it more easy.
308              
309             [% IF c.form.has_error %]
310             <p>Input Error</p>
311             <ul>
312             [% FOREACH key IN c.form.error %]
313             [% FOREACH type IN c.form.error(key) %]
314             <li>Invalid: [% key %] - [% type %]</li>
315             [% END %]
316             [% END %]
317             </li>
318             [% END %]
319              
320             And you can also use messages configuration as hash reference.
321              
322             plugin_validator => {
323             plugins => [...],
324             messages => {
325             user => {
326             name => {
327             NOT_BLANK => 'Input name!',
328             ASCII => 'Input name with ascii code!',
329             },
330             email => {
331             DEFAULT => 'email is wrong.!',
332             NOT_BLANK => 'input email.!'
333             },
334             },
335             company => {
336             name => {
337             NOT_BLANK => 'Input name!',
338             },
339             },
340             },
341             },
342              
343             or YAML file. set file name
344              
345             plugin_validator => {
346             plugins => [...],
347             messages => 'conf/messages.yml',
348             },
349              
350             and prepare yaml file like following,
351              
352             DEFAULT:
353             name:
354             DEFAULT: name is invalid
355             user:
356             name:
357             NOT_BLANK: Input name!
358             ASCII: Input name with ascii code!
359             email:
360             DEFAULT: Email is wrong!
361             NOT_BLANK: Input email!
362             company:
363             name:
364             NOT_BLANK: Input name!
365              
366             the format is...
367              
368             Action1_Name:
369             Key1_Name:
370             Validation1_Name: Message
371             Validation2_Name: Message
372             Key2_Name:
373             Validation1_Name: Message
374             Action2_Name:
375             Key1_Name:
376             ...
377            
378             After messages configuration, call messages() method from result-object.
379             and set action-name as argument.
380              
381             [% IF c.form.has_error %]
382             <ul>
383             [% FOREACH message IN c.form.messages('user') %]
384             <li>[% message %]</li>
385             [% END %]
386             </ul>
387             [% END %]
388              
389             you can set each message format
390              
391             MyApp->config(
392             validator => {
393             messages => 'messages.yml',
394             message_format => '<p>%s</p>'
395             },
396             );
397              
398             [% IF c.form.has_error %]
399             [% c.form.messages('user').join("\n") %]
400             [% END %]
401              
402             =head1 SEE ALSO
403              
404             L<FormValidator::Simple>,
405             L<Catalyst::Plugin::FormValidator::Simple>,
406             L<Egg::Release>,
407              
408             =head1 AUTHOR
409              
410             This code is a transplant of 'Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>'
411             of the code of 'L<Catalyst::Plugin::FormValidator::Simple>'.
412              
413             Therefore, the copyright of this code is assumed to be the one that belongs
414             to 'Lyo Kato E<lt>lyo.kato@gmail.comE<gt>'.
415              
416             =head1 COPYRIGHT AND LICENSE
417              
418             Copyright(C) 2005 by Lyo Kato
419              
420             This library is free software; you can redistribute it and/or
421             modify it under the same terms as Perl itself.
422              
423             =cut