first commit
admin [Sat, 5 Jun 2010 03:20:38 +0000 (21:20 -0600)]
Changes [new file with mode: 0644]
MANIFEST [new file with mode: 0644]
META.yml [new file with mode: 0644]
Makefile.PL [new file with mode: 0644]
README [new file with mode: 0644]
lib/Catalyst/Plugin/FormValidator/Simple.pm [new file with mode: 0644]
t/01_use.t [new file with mode: 0644]
t/02_pod.t [new file with mode: 0644]

diff --git a/Changes b/Changes
new file mode 100644 (file)
index 0000000..623c0f3
--- /dev/null
+++ b/Changes
@@ -0,0 +1,58 @@
+Revision history for Perl extension Catalyst::Plugin::FormValidator::Simple.
+
+0.13  Thu Sep  3 14:22:00 2009
+    - added message_decode_from option corresponding to
+      FormValidator::Simple set_message_decode_from option.
+      Thanks to Hu Hailin.
+
+0.12  Thu Nov  2 18:06:00 2006
+    - revert to ver 0.10.
+      When you use relative path on config-file,
+      it's better to write like follow example.
+
+      validator:
+        profiles: __path_to(profiles.yml)__
+        messages: __path_to(messages.yml)__
+
+      Thanks to TYPESTER and Hideo Kimura.
+
+0.11  Wed Nov  1 17:14:00 2006
+    - fixed to deal with messages file's path well.
+      you can set relative filepath in mod_perl environment.
+      Thanks to Hideo Kimura
+
+0.10  Mon May 15 00:00:00 2006
+    - bugfix: Now, it validates parameters correctly,
+      when they're set with $c->req->param on recent version of Catalyst.
+      (Thanks to luo.dongxiang)
+
+0.09  Fri Mar 3 00:00:00 2006
+    - fixed poor mistake, when passing option setting.
+      (Thanks to Roman Filippov)
+
+0.08  Mon Feb 27 00:00:00 2006
+    - Now, this doesn't use NEXT::ACTUAL.
+          That's make test-server not to work as expected on restarting.
+
+0.06  Mon Nov 18 00:00:00 2005
+    - change the version of prerequired FormValidator::Simple.
+      Now this module requires FormValidator::Simple Version 0.10 or over.
+      And added many document and tutorials.
+
+0.05  Mon Nov 14 08:54:00 2005
+    - fixed documentation
+      (Thanks to John Wang)
+
+0.04  Thu Oct 20 12:23:40 2005
+    - fixed bug: option setting wasn't passed to validator.
+
+0.03  Sat Oct  1 10:23:40 2005
+    - fixed documentation
+
+0.02  Fri Sep 30 10:23:40 2005
+    - cpan release version
+
+0.01  Tue Jul  5 10:23:40 2005
+    - original version; created by h2xs 1.23 with options
+        -X -A Catalyst::Plugin::FormValidator::Simple
+
diff --git a/MANIFEST b/MANIFEST
new file mode 100644 (file)
index 0000000..3c59c19
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,8 @@
+Changes
+lib/Catalyst/Plugin/FormValidator/Simple.pm
+Makefile.PL
+MANIFEST
+META.yml                       Module meta-data (added by MakeMaker)
+README
+t/01_use.t
+t/02_pod.t
diff --git a/META.yml b/META.yml
new file mode 100644 (file)
index 0000000..dd200c7
--- /dev/null
+++ b/META.yml
@@ -0,0 +1,23 @@
+--- #YAML:1.0
+name:               Catalyst-Plugin-FormValidator-Simple
+version:            0.13
+abstract:           ~
+author:  []
+license:            unknown
+distribution_type:  module
+configure_requires:
+    ExtUtils::MakeMaker:  0
+build_requires:
+    ExtUtils::MakeMaker:  0
+requires:
+    Catalyst:             5.3
+    Catalyst::Plugin::FormValidator:  0
+    FormValidator::Simple:  0.28
+no_index:
+    directory:
+        - t
+        - inc
+generated_by:       ExtUtils::MakeMaker version 6.50
+meta-spec:
+    url:      http://module-build.sourceforge.net/META-spec-v1.4.html
+    version:  1.4
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644 (file)
index 0000000..122c521
--- /dev/null
@@ -0,0 +1,12 @@
+use ExtUtils::MakeMaker;
+# See lib/ExtUtils/MakeMaker.pm for details of how to influence
+# the contents of the Makefile that is written.
+WriteMakefile(
+    NAME              => 'Catalyst::Plugin::FormValidator::Simple',
+    VERSION_FROM      => 'lib/Catalyst/Plugin/FormValidator/Simple.pm', # finds $VERSION
+    PREREQ_PM         => {
+        Catalyst => 5.30,
+        FormValidator::Simple => 0.28,
+        Catalyst::Plugin::FormValidator => 0,
+    }, # e.g., Module::Name => 1.1
+);
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..019a5dd
--- /dev/null
+++ b/README
@@ -0,0 +1,34 @@
+Catalyst-Plugin-FormValidator-Simple version 0.12
+=================================================
+
+The README is used to introduce the module and provide instructions on
+how to install the module, any machine dependencies it may have (for
+example C compilers and installed libraries) and any other information
+that should be provided before the module is installed.
+
+A README file is required for CPAN modules since CPAN extracts the
+README file from a module distribution so that people browsing the
+archive can use it get an idea of the modules uses. It is usually a
+good idea to provide version information here so that people can
+decide whether fixes for the module are worth downloading.
+
+INSTALLATION
+
+To install this module type the following:
+
+   perl Makefile.PL
+   make
+   make test
+   make install
+
+COPYRIGHT AND LICENCE
+
+Put the correct copyright and licence information here.
+
+Copyright (C) 2005 by Lyo Kato
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8.5 or,
+at your option, any later version of Perl 5 you may have available.
+
+
diff --git a/lib/Catalyst/Plugin/FormValidator/Simple.pm b/lib/Catalyst/Plugin/FormValidator/Simple.pm
new file mode 100644 (file)
index 0000000..77ff0dd
--- /dev/null
@@ -0,0 +1,463 @@
+package Catalyst::Plugin::FormValidator::Simple;
+use strict;
+use base qw/Catalyst::Plugin::FormValidator/;
+# doesn't use parent module at all, but this is required for Catalyst::Plugin::FillInForm
+
+use NEXT;
+require FormValidator::Simple;
+
+our $VERSION = '0.13';
+
+sub setup {
+    my $self = shift;
+    $self->NEXT::setup(@_);
+    my $setting = $self->config->{validator};
+    my $plugins = $setting && exists $setting->{plugins}
+        ? $setting->{plugins}
+        : [];
+        FormValidator::Simple->import(@$plugins);
+    if ( $setting && exists $setting->{messages} ) {
+        FormValidator::Simple->set_messages( $setting->{messages} );
+    }
+    if ( $setting && exists $setting->{options} ) {
+        FormValidator::Simple->set_option( %{ $setting->{options} } );
+    }
+    if ( $setting && exists $setting->{message_format} ) {
+        FormValidator::Simple->set_message_format( $setting->{message_format} );
+    }
+    if ( $setting && exists $setting->{message_decode_from} ) {
+        FormValidator::Simple->set_message_decode_from( $setting->{message_decode_from} );
+    }
+}
+
+sub prepare {
+    my $c = shift;
+    $c = $c->NEXT::prepare(@_);
+    $c->{validator} = FormValidator::Simple->new;
+    return $c;
+}
+
+sub form {
+    my $c = shift;
+    if ($_[0]) {
+        my $form = $_[1] ? [@_] : $_[0];
+        $c->{validator}->check($c->req, $form);
+    }
+    return $c->{validator}->results;
+}
+
+sub set_invalid_form {
+    my $c = shift;
+    $c->{validator}->set_invalid(@_);
+    return $c->{validator}->results;
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Catalyst::Plugin::FormValidator::Simple - Validator for Catalyst with FormValidator::Simple
+
+=head1 SYNOPSIS
+
+    use Catalyst qw/FormValidator::Simple FillInForm/;
+
+    # set option
+    MyApp->config->{validator} = {
+        plugins => ['CreditCard', 'Japanese'],
+        options => { charset => 'euc'},
+    }
+
+in your controller
+
+    sub defaulti : Private {
+
+        my ($self, $c) = @_;
+
+        $c->form(
+            param1 => [qw/NOT_BLANK ASCII/, [qw/LENGTH 4 10/]],
+            param2 => [qw/NOT_BLANK/, [qw/JLENGTH 4 10/]],
+            mail1  => [qw/NOT_BLANK EMAIL_LOOSE/],
+            mail2  => [qw/NOT_BLANK EMAIL_LOOSE/],
+            { mail => [qw/mail1 mail2/] } => ['DUPLICATION'],
+        );
+
+        print $c->form->valid('param1');
+
+        if ( some condition... ) {
+
+            $c->form(
+                other_param => [qw/NOT_INT/],
+            );
+        }
+
+        if ( some condition... ) {
+
+            # set your original invalid type.
+            $c->set_invalid_form( param3 => 'MY_ERROR' );
+
+        }
+
+        if ( $c->form->has_error ) {
+            
+            if ( $c->form->missing('param1') ) {
+                ...
+            }
+
+            if ( $c->form->invalid( param1 => 'ASCII' ) ) {
+                ...
+            }
+
+            if ( $c->form->invalid( param3 => 'MY_ERROR' ) ) {
+                ...
+            }
+
+        }
+    }
+
+=head1 DESCRIPTION
+
+This plugin allows you to validate request parameters with FormValidator::Simple.
+See L<FormValidator::Simple> for more information.
+
+This behaves like as L<Catalyst::Plugin::FormValidator>.
+
+=head1 CONFIGURATION
+
+set config with 'validator' key.
+
+    MyApp->config->{validator} = { ... };
+
+or
+
+    MyApp->config(
+        validator => { ... },
+    );
+
+=head2 PLUGINS
+
+If you want to use some plugins for FormValidator::Simple, you can set like following.
+
+    MyApp->config(
+        validator => {
+            plugins => [qw/Japanese CreditCard DBIC::Unique/],
+        },
+    );
+
+In this example, FormValidator::Simple::Plugin::Japanese, FormValidator::Simple::Plugin::CreditCard,
+and FormValidator::Simple::Plugin::DBIC::Unique are loaded.
+
+=head2 OPTIONS
+
+When you set some options needed by specific validations, do like this.
+
+    MyApp->config(
+        validator => {
+            plugins => [qw/Japanese CreditCard DBIC::Unique/],
+            options => {
+                charset => 'euc',
+                dbic_base_class => 'MyApp::Model::DBIC',
+            },
+        },
+    );
+
+'charset' is necessary for Plugin::Japanese, and 'dbic_cbase_class' is used in Plugin::DBIC::Unique.
+
+=head1 VALIDATION
+
+use 'form' method, see L<FormValidator::Simple> in detail.
+
+    sub do_add : Local {
+        my ( $self, $c ) = @_;
+
+        # execute validation.
+        $c->form(
+            name  => [qw/NOT_BLANK ASCII/,       [qw/LENGTH 0 20/] ],
+            email => [qw/NOT_BLANK EMAIL_LOOSE/, [qw/LENGTH 0 20/] ],
+            { unique => [qw/name email/] } => [qw/DBIC_UNIQUE User name email/],
+        );
+
+        if ( ... ) {
+
+            # execute validation one more time in specific condition.
+            $c->form(
+                ...                 
+            );
+
+        }
+
+        # See Catalyst::Plugin::RequestToken for '$c->validate_token'
+        if ( $c->validate_token ) {
+
+            # you can force to set invalid data.
+            $c->set_invalid_form( token => 'TOKEN' );
+
+        }
+
+        # check result.
+        # you can pick up result-object with 'form' method
+
+        my $result = $c->form;
+
+        if ( $result->has_error ) {
+
+        # this is same as
+        # if ( $result->has_missing or $result->has_invalid )
+
+            $c->detach('add');
+
+        }
+
+    }
+
+=head1 HANDLING SUCCESSFUL RESULT
+
+After it passes all validations, you may wanna put input-data into database.
+It's a elegant way to use [ L<Class::DBI> and L<Class::DBI::FromForm> ] or [ L<DBIx::Class> and L<DBIx::Class::WebForm> ].
+
+    sub do_add : Local {
+        my ( $self, $c ) = @_;
+
+        $c->form(
+            name  => [qw/NOT_BLANK/],
+            email => [qw/NOT_BLANK/],
+        );
+
+        my $result = $c->form;
+        if ( $result->has_error ) {
+            $c->detach('add');
+        }
+
+        my $user = MyProj::Model::DBIC::User->create_from_form($result);
+        
+        # this behaves like this...
+        # MyProj::Model::DBIC::User->create({
+        #    name  => $result->valid('name'),
+        #    email => $result->valid('email'),
+        # });
+        #
+        # if the key exists as the table's column, set the value with 'valid'
+    }
+
+Here, I explain about 'valid' method. If the value indicated with key-name passes validations,
+You can get the data with 'valid',
+
+    my $result = $c->form(
+        name  => [qw/NOT_BLANK/],
+        email => [qw/NOT_BLANK/],
+    ); 
+
+    print $result->valid('name');
+
+    print $result->valid('email');
+
+But, this is for only single key validation normally.
+
+    my $result = $c->form(
+        name => [qw/NOT_BLANK/], # single key validation
+        { mail_dup => [qw/email email2/] } => ['DUPLICATION'] # multiple keys one
+    );
+
+    print $result->valid('name'); # print out the value of 'name'
+
+    print $result->valid('mail_dup'); # no value.
+
+There are exceptions. These are 'DATETIME', 'DATE'.
+
+    my $result = $c->form(
+        { created_on => [qw/created_year created_month created_day/] }
+        =>
+        [qw/DATETIME/],
+    );
+
+    print $result->valid('created_on'); #print out datetime string like "2005-11-23 00:00:00".
+
+If you set some class around datetime in configuration. It returns object of the class you indicate.
+You can choose from L<Time::Piece> and L<DateTime>. For example...
+
+    MyApp->config(
+        validator => {
+            plugins => [...],
+            options => {
+                datetime_class => 'Time::Piece',
+            },
+        },
+    );
+
+or
+
+    MyApp->config(
+        validator => {
+            plugins => [...],
+            options => {
+                datetime_class => 'DateTime',
+                time_zone      => 'Asia/Tokyo',
+            },
+        },
+    );
+
+then
+
+    my $result = $c->form(
+        { created_on => [qw/created_year created_month created_day/] }
+        =>
+        [qw/DATETIME/],
+    );
+
+    my $dt = $result->valid('created_on');
+
+    print $dt->ymd;
+
+    MyProj::Model::CDBI::User->create_from_form($result);
+
+This may be useful when you define 'has_a' relation for datetime columns.
+For example, in your table class inherits 'Class::DBI'
+
+    __PACKAGE__->has_a( created_on => 'DateTime',
+        inflate => ...,
+        deflate => ...,
+    );
+
+And see also L<Class::DBI::Plugin::TimePiece>, L<Class::DBI::Plugin::DateTime>.
+
+=head1 MESSAGE HANDLING
+
+in template file, you can handle it in detail.
+
+    [% IF c.form.has_error %]
+    <p>Input Error</p>
+    <ul>
+    [% IF c.form.missing('name') %]
+    <li>input name!</li>
+    [% END %]
+    [% IF c.form.invalid('name') %]
+    <li>name is wrong</li>
+    [% END %]
+    [% IF c.form.invalid('name', 'ASCII') %]
+    <li>input name with ascii code.</li>
+    [% END %]
+    [% IF c.form.invalid('name', 'LENGTH') %]
+    <li>wrong length for name.</li>
+    [% END %]
+    </ul>
+    [% END %]
+
+or, make it more easy.
+
+    [% IF c.form.has_error %]
+    <p>Input Error</p>
+    <ul>
+    [% FOREACH key IN c.form.error %]
+        [% FOREACH type IN c.form.error(key) %]
+        <li>Invalid: [% key %] - [% type %]</li>
+        [% END %]
+    [% END %]
+    </li>
+    [% END %]
+
+And you can also use messages configuration as hash reference.
+
+    MyApp->config(
+        validator => {
+            plugins  => [...],
+            messages => {
+                user => {
+                    name => {
+                        NOT_BLANK => 'Input name!',
+                        ASCII     => 'Input name with ascii code!',
+                    },
+                    email => {
+                        DEFAULT   => 'email is wrong.!',
+                        NOT_BLANK => 'input email.!'
+                    },
+                },
+                company => {
+                    name => {
+                        NOT_BLANK => 'Input name!',
+                    },
+                },
+            },
+        },
+    );
+
+or YAML file. set file name
+
+    MyApp->config(
+        validator => {
+            plugins  => [...],
+            messages => 'conf/messages.yml',
+        },
+    );
+
+and prepare yaml file like following,
+
+    DEFAULT:
+        name:
+            DEFAULT: name is invalid
+    user:
+        name:
+            NOT_BLANK: Input name!
+            ASCII: Input name with ascii code!
+        email:
+            DEFAULT: Email is wrong!
+            NOT_BLANK: Input email!
+    company:
+        name:
+            NOT_BLANK: Input name!
+
+the format is...
+
+    Action1_Name:
+        Key1_Name:
+            Validation1_Name: Message
+            Validation2_Name: Message
+        Key2_Name:
+            Validation1_Name: Message
+    Action2_Name:
+        Key1_Name:
+            ...
+        
+After messages configuration, call messages() method from result-object.
+and set action-name as argument.
+
+    [% IF c.form.has_error %]
+    <ul>
+        [% FOREACH message IN c.form.messages('user') %]
+        <li>[% message %]</li>
+        [% END %]
+    </ul>
+    [% END %]
+
+you can set each message format
+
+    MyApp->config(
+        validator => {
+            messages => 'messages.yml',  
+            message_format => '<p>%s</p>'
+        },
+    );
+
+    [% IF c.form.has_error %]
+        [% c.form.messages('user').join("\n") %]
+    [% END %]
+
+=head1 SEE ALSO
+
+L<FormValidator::Simple>
+
+L<Catalyst>
+
+=head1 AUTHOR
+
+Lyo Kato E<lt>lyo.kato@gmail.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright(C) 2005 by Lyo Kato
+
+This library is free software; you can redistribute it and/or
+modify it under the same terms as Perl itself.
+
+=cut
+
diff --git a/t/01_use.t b/t/01_use.t
new file mode 100644 (file)
index 0000000..ca4058c
--- /dev/null
@@ -0,0 +1,4 @@
+use strict;
+use Test::More tests => 1;
+
+BEGIN { use_ok('Catalyst::Plugin::FormValidator::Simple') }
diff --git a/t/02_pod.t b/t/02_pod.t
new file mode 100644 (file)
index 0000000..e344479
--- /dev/null
@@ -0,0 +1,5 @@
+use Test::More;
+eval "use Test::Pod 1.14";
+plan skip_all => 'Test::Pod 1.14 required' if $@;
+plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
+all_pod_files_ok();