Permalink
Browse files

* Added nest forward validation (Thanks to Daniel Burke <daniel.burk…

…e@gaiax.com>)

 * Added form_messages method


git-svn-id: http://svn.unknownplace.org/public/library/perl/trunk/Catalyst-Plugin-FormValidator-Simple-Auto@446 325430d6-99fa-0310-bb67-a757393f56df
  • Loading branch information...
typester
typester committed Feb 22, 2007
1 parent ea456fd commit f81d68c9fe9ab7adc10bf50945a4cf9fbd738c0c
Showing with 193 additions and 5 deletions.
  1. +5 −0 Changes
  2. +1 −1 Makefile.PL
  3. +10 −0 README
  4. +36 −4 lib/Catalyst/Plugin/FormValidator/Simple/Auto.pm
  5. +62 −0 t/05_form_messages.t
  6. +79 −0 t/06_nested_actions.t
View
@@ -1,5 +1,10 @@
Revision history for Perl module Catalyst::Plugin::FormValidator::Simple::Auto
+0.02 2007-02-22T17:14:20+09:00
+ - Added nest forward validation
+ (Thanks to Daniel Burke <daniel.burke@gaiax.com>)
+ - Added form_messages method
+
0.11 2007-01-02T02:56:00+09:00
- Fixed cannot handle error_messages when validation rule is array ref
View
@@ -5,7 +5,7 @@ all_from 'lib/Catalyst/Plugin/FormValidator/Simple/Auto.pm';
build_requires 'Test::More';
requires 'Catalyst' => '5.30';
-requires 'Catalyst::Plugin::FormValidator::Simple';
+requires 'Catalyst::Plugin::FormValidator::Simple' => '0.20';
requires 'UNIVERSAL::isa';
requires 'YAML';
View
10 README
@@ -75,6 +75,16 @@ BUNDLING ERROR MESSAGES
- rule: NOT_BLANK
message: param1 is required!
+METHODS
+ validator_profile
+ An accessor for current profile name
+
+ form_messages
+ form_messages( 'key' );
+ Return error messages about current validator profile.
+
+ If the key isn't presented, return all messages as hash.
+
EXTENDED METHODS
setup
prepare
@@ -6,8 +6,9 @@ use base qw/Class::Accessor::Fast/;
use Catalyst::Exception;
use UNIVERSAL::isa;
use YAML;
+use FormValidator::Simple;
-our $VERSION = '0.11';
+our $VERSION = '0.12';
__PACKAGE__->mk_accessors(qw/validator_profile/);
@@ -89,6 +90,28 @@ Above two configs is equals to:
- rule: NOT_BLANK
message: param1 is required!
+=head1 METHODS
+
+=head2 validator_profile
+
+An accessor for current profile name
+
+=head2 form_messages
+
+=head2 form_messages( 'key' );
+
+Return error messages about current validator profile.
+
+If the key isn't presented, return all messages as hash.
+
+=cut
+
+sub form_messages {
+ my ($c, $key) = @_;
+ my $messages = $c->form->field_messages( $c->validator_profile );
+
+ $key ? $messages->{$key} : $messages;
+}
=head1 EXTENDED METHODS
@@ -171,13 +194,22 @@ sub forward {
my $c = shift;
my $action = $c->dispatcher->_invoke_as_path($c, @_);
+ local $NEXT::NEXT{ $c, 'forward' };
+
+ my $res;
if ( my $profile = $c->config->{validator}{profiles}{ $action } ) {
- $c->validator_profile($action);
+ # We only need to create a new validator if there is a new profile
+ local $c->{validator} = FormValidator::Simple->new;
+ local $c->{validator_profile} = $action;
+
$c->form(%$profile);
+ $res = $c->NEXT::forward(@_);
+ }
+ else {
+ $res = $c->NEXT::forward(@_);
}
- local $NEXT::NEXT{ $c, 'forward' };
- $c->NEXT::forward(@_);
+ $res;
}
=head1 ORIGINAL IDEA
View
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+{
+ package TestApp;
+
+ use Catalyst qw/
+ FormValidator::Simple
+ FormValidator::Simple::Auto
+ /;
+
+ __PACKAGE__->config(
+ name => 'TestApp',
+ validator => {
+ profiles => {
+ action1 => {
+ param1 => [
+ { rule => 'NOT_BLANK', message => 'NOT_BLANK!!!' },
+ { rule => 'ASCII', message => 'ASCII!!!' },
+ ],
+ },
+ },
+ },
+ );
+ __PACKAGE__->setup;
+
+ sub action1 : Global {
+ my ( $self, $c ) = @_;
+
+ if ($c->form->has_error) {
+ if (($c->req->params->{as} || '') eq 'hash') {
+ $c->res->body( $c->form_messages->{param1}->[0] );
+ }
+ else {
+ $c->res->body( $c->form_messages('param1')->[0] );
+ }
+ }
+ else {
+ $c->res->body('no errors');
+ }
+ }
+}
+
+use Catalyst::Test 'TestApp';
+use Test::Base;
+
+plan tests => 8;
+
+ok( my $res = request('/action1'), 'request ok' );
+is( $res->content, 'NOT_BLANK!!!', 'is NOT_BLANK error');
+
+ok( $res = request('/action1?as=hash'), 'request ok' );
+is( $res->content, 'NOT_BLANK!!!', 'is NOT_BLANK error (test as hash)');
+
+
+ok( $res = request('/action1?param1=aaa bbb'), 'request ok' );
+is( $res->content, 'ASCII!!!', 'is ASCII error');
+
+ok( $res = request('/action1?param1=aaa bbb&as=hash'), 'request ok' );
+is( $res->content, 'ASCII!!!', 'is ASCII error (test as hash)');
View
@@ -0,0 +1,79 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+{
+ package TestApp;
+
+ use Catalyst qw/
+ FormValidator::Simple
+ FormValidator::Simple::Auto
+ /;
+
+ __PACKAGE__->config(
+ name => 'TestApp',
+ validator => {
+ profiles => {
+ action_a => { input => [ { rule => 'NOT_BLANK', message => 'error a' }, 'INT'] },
+ action_b => { input => [ { rule => 'NOT_BLANK', message => 'error b' }, 'ASCII' ] },
+ action_c => { input => [] },
+ },
+ },
+ );
+ __PACKAGE__->setup;
+
+ sub action_a : Local {
+ my ($self,$c) = @_;
+
+ if ($c->form->has_error) {
+ $c->forward('action_b');
+ }
+ else {
+ $c->res->body('a');
+ }
+
+ if (($c->req->params->{restore} || '') eq 'a') {
+ $c->res->body( $c->form_messages->{input}->[0] );
+ }
+ }
+
+ sub action_b : Local {
+ my ($self,$c) = @_;
+
+ if ($c->form->has_error) {
+ $c->forward('action_c');
+ }
+ else {
+ $c->res->body('b');
+ }
+
+ if (($c->req->params->{restore} || '') eq 'b') {
+ $c->res->body( $c->form->field_messages( $c->validator_profile )->{input}[0] );
+ }
+ }
+
+ sub action_c : Local {
+ my ($self,$c) = @_;
+
+ if ($c->form->has_error) {
+ $c->res->body('error');
+ }
+ else {
+ $c->res->body('c');
+ }
+ }
+}
+
+use Catalyst::Test 'TestApp';
+use Test::Base;
+
+plan tests => '5';
+
+is( get('/action_a?input=123'), 'a', 'check a, normal validation');
+is( get('/action_a?input=abc'), 'b', 'check b, nest level 1');
+is( get('/action_a?input='), 'c', 'check c, nest level 2');
+
+is( get('/action_a?input=&restore=a'), 'error a', 'check restored form objects');
+is( get('/action_a?input=&restore=b'), 'error b', 'check restored form objects');
+

0 comments on commit f81d68c

Please sign in to comment.