1 package Catalyst::Plugin::FormValidator::Simple;
3 use base qw/Catalyst::Plugin::FormValidator/;
4 # doesn't use parent module at all, but this is required for Catalyst::Plugin::FillInForm
7 require FormValidator::Simple;
13 $self->maybe::next::method(@_);
14 my $setting = $self->config->{validator};
15 my $plugins = $setting && exists $setting->{plugins}
18 FormValidator::Simple->import(@$plugins);
19 if ( $setting && exists $setting->{messages} ) {
20 FormValidator::Simple->set_messages( $setting->{messages} );
22 if ( $setting && exists $setting->{options} ) {
23 FormValidator::Simple->set_option( %{ $setting->{options} } );
25 if ( $setting && exists $setting->{message_format} ) {
26 FormValidator::Simple->set_message_format( $setting->{message_format} );
28 if ( $setting && exists $setting->{message_decode_from} ) {
29 FormValidator::Simple->set_message_decode_from( $setting->{message_decode_from} );
35 $c = $c->maybe::next::method(@_);
36 $c->{validator} = FormValidator::Simple->new;
43 my $form = $_[1] ? [@_] : $_[0];
44 $c->{validator}->check($c->req, $form);
46 return $c->{validator}->results;
49 sub set_invalid_form {
51 $c->{validator}->set_invalid(@_);
52 return $c->{validator}->results;
60 Catalyst::Plugin::FormValidator::Simple - Validator for Catalyst with FormValidator::Simple
64 use Catalyst qw/FormValidator::Simple FillInForm/;
67 MyApp->config->{validator} = {
68 plugins => ['CreditCard', 'Japanese'],
69 options => { charset => 'euc'},
74 sub defaulti : Private {
79 param1 => [qw/NOT_BLANK ASCII/, [qw/LENGTH 4 10/]],
80 param2 => [qw/NOT_BLANK/, [qw/JLENGTH 4 10/]],
81 mail1 => [qw/NOT_BLANK EMAIL_LOOSE/],
82 mail2 => [qw/NOT_BLANK EMAIL_LOOSE/],
83 { mail => [qw/mail1 mail2/] } => ['DUPLICATION'],
86 print $c->form->valid('param1');
88 if ( some condition... ) {
91 other_param => [qw/NOT_INT/],
95 if ( some condition... ) {
97 # set your original invalid type.
98 $c->set_invalid_form( param3 => 'MY_ERROR' );
102 if ( $c->form->has_error ) {
104 if ( $c->form->missing('param1') ) {
108 if ( $c->form->invalid( param1 => 'ASCII' ) ) {
112 if ( $c->form->invalid( param3 => 'MY_ERROR' ) ) {
121 This plugin allows you to validate request parameters with FormValidator::Simple.
122 See L<FormValidator::Simple> for more information.
124 This behaves like as L<Catalyst::Plugin::FormValidator>.
128 set config with 'validator' key.
130 MyApp->config->{validator} = { ... };
135 validator => { ... },
140 If you want to use some plugins for FormValidator::Simple, you can set like following.
144 plugins => [qw/Japanese CreditCard DBIC::Unique/],
148 In this example, FormValidator::Simple::Plugin::Japanese, FormValidator::Simple::Plugin::CreditCard,
149 and FormValidator::Simple::Plugin::DBIC::Unique are loaded.
153 When you set some options needed by specific validations, do like this.
157 plugins => [qw/Japanese CreditCard DBIC::Unique/],
160 dbic_base_class => 'MyApp::Model::DBIC',
165 'charset' is necessary for Plugin::Japanese, and 'dbic_cbase_class' is used in Plugin::DBIC::Unique.
169 use 'form' method, see L<FormValidator::Simple> in detail.
172 my ( $self, $c ) = @_;
174 # execute validation.
176 name => [qw/NOT_BLANK ASCII/, [qw/LENGTH 0 20/] ],
177 email => [qw/NOT_BLANK EMAIL_LOOSE/, [qw/LENGTH 0 20/] ],
178 { unique => [qw/name email/] } => [qw/DBIC_UNIQUE User name email/],
183 # execute validation one more time in specific condition.
190 # See Catalyst::Plugin::RequestToken for '$c->validate_token'
191 if ( $c->validate_token ) {
193 # you can force to set invalid data.
194 $c->set_invalid_form( token => 'TOKEN' );
199 # you can pick up result-object with 'form' method
201 my $result = $c->form;
203 if ( $result->has_error ) {
206 # if ( $result->has_missing or $result->has_invalid )
214 =head1 HANDLING SUCCESSFUL RESULT
216 After it passes all validations, you may wanna put input-data into database.
217 It's a elegant way to use [ L<Class::DBI> and L<Class::DBI::FromForm> ] or [ L<DBIx::Class> and L<DBIx::Class::WebForm> ].
220 my ( $self, $c ) = @_;
223 name => [qw/NOT_BLANK/],
224 email => [qw/NOT_BLANK/],
227 my $result = $c->form;
228 if ( $result->has_error ) {
232 my $user = MyProj::Model::DBIC::User->create_from_form($result);
234 # this behaves like this...
235 # MyProj::Model::DBIC::User->create({
236 # name => $result->valid('name'),
237 # email => $result->valid('email'),
240 # if the key exists as the table's column, set the value with 'valid'
243 Here, I explain about 'valid' method. If the value indicated with key-name passes validations,
244 You can get the data with 'valid',
246 my $result = $c->form(
247 name => [qw/NOT_BLANK/],
248 email => [qw/NOT_BLANK/],
251 print $result->valid('name');
253 print $result->valid('email');
255 But, this is for only single key validation normally.
257 my $result = $c->form(
258 name => [qw/NOT_BLANK/], # single key validation
259 { mail_dup => [qw/email email2/] } => ['DUPLICATION'] # multiple keys one
262 print $result->valid('name'); # print out the value of 'name'
264 print $result->valid('mail_dup'); # no value.
266 There are exceptions. These are 'DATETIME', 'DATE'.
268 my $result = $c->form(
269 { created_on => [qw/created_year created_month created_day/] }
274 print $result->valid('created_on'); #print out datetime string like "2005-11-23 00:00:00".
276 If you set some class around datetime in configuration. It returns object of the class you indicate.
277 You can choose from L<Time::Piece> and L<DateTime>. For example...
283 datetime_class => 'Time::Piece',
294 datetime_class => 'DateTime',
295 time_zone => 'Asia/Tokyo',
302 my $result = $c->form(
303 { created_on => [qw/created_year created_month created_day/] }
308 my $dt = $result->valid('created_on');
312 MyProj::Model::CDBI::User->create_from_form($result);
314 This may be useful when you define 'has_a' relation for datetime columns.
315 For example, in your table class inherits 'Class::DBI'
317 __PACKAGE__->has_a( created_on => 'DateTime',
322 And see also L<Class::DBI::Plugin::TimePiece>, L<Class::DBI::Plugin::DateTime>.
324 =head1 MESSAGE HANDLING
326 in template file, you can handle it in detail.
328 [% IF c.form.has_error %]
331 [% IF c.form.missing('name') %]
334 [% IF c.form.invalid('name') %]
335 <li>name is wrong</li>
337 [% IF c.form.invalid('name', 'ASCII') %]
338 <li>input name with ascii code.</li>
340 [% IF c.form.invalid('name', 'LENGTH') %]
341 <li>wrong length for name.</li>
346 or, make it more easy.
348 [% IF c.form.has_error %]
351 [% FOREACH key IN c.form.error %]
352 [% FOREACH type IN c.form.error(key) %]
353 <li>Invalid: [% key %] - [% type %]</li>
359 And you can also use messages configuration as hash reference.
367 NOT_BLANK => 'Input name!',
368 ASCII => 'Input name with ascii code!',
371 DEFAULT => 'email is wrong.!',
372 NOT_BLANK => 'input email.!'
377 NOT_BLANK => 'Input name!',
384 or YAML file. set file name
389 messages => 'conf/messages.yml',
393 and prepare yaml file like following,
397 DEFAULT: name is invalid
400 NOT_BLANK: Input name!
401 ASCII: Input name with ascii code!
403 DEFAULT: Email is wrong!
404 NOT_BLANK: Input email!
407 NOT_BLANK: Input name!
413 Validation1_Name: Message
414 Validation2_Name: Message
416 Validation1_Name: Message
421 After messages configuration, call messages() method from result-object.
422 and set action-name as argument.
424 [% IF c.form.has_error %]
426 [% FOREACH message IN c.form.messages('user') %]
427 <li>[% message %]</li>
432 you can set each message format
436 messages => 'messages.yml',
437 message_format => '<p>%s</p>'
441 [% IF c.form.has_error %]
442 [% c.form.messages('user').join("\n") %]
447 L<FormValidator::Simple>
453 Lyo Kato E<lt>lyo.kato@gmail.comE<gt>
455 =head1 COPYRIGHT AND LICENSE
457 Copyright(C) 2005 by Lyo Kato
459 This library is free software; you can redistribute it and/or
460 modify it under the same terms as Perl itself.