Skip to content

Instantly share code, notes, and snippets.

@mash
Created February 25, 2009 12:25
Show Gist options
  • Save mash/70143 to your computer and use it in GitHub Desktop.
Save mash/70143 to your computer and use it in GitHub Desktop.
use Test::Base qw/no_plan/;
{
package TestApp;
use Catalyst qw/
FormValidator::Simple
/;
use JSON;
__PACKAGE__->config(
name => 'TestApp',
validator => {
forms => {
user_regist_form => [ 'user_name', 'user_url' ],
user_modify_form => [ 'user_name', 'user_url' ],
user_modify_form2 => [ 'user_name', 'user_url' ],
user_descri_form => [ 'user_description' ],
},
columns => {
user_name => [
{
rule => 'NOT_BLANK',
message => 'NOT_BLANK!',
},{
rule => 'ASCII',
message => 'ASCII!',
},{
self_rule => 'MY_ORIGINAL',
message => 'MY_ORIGINAL!',
}
],
user_url => [
{
rule => 'ASCII',
message => 'ASCII!',
}
],
user_description => [
{
rule => [ 'LENGTH', 0, 10 ],
message => 'TOO LONG!',
}
],
},
},
);
__PACKAGE__->setup;
sub action1 :Global {
my ( $self, $c ) = @_;
if ( ! $c->forward( 'user_regist_form' ) ) {
$c->res->body( to_json($c->form->field_messages('user_regist_form')) );
}
else {
$c->res->body('no errors');
}
}
sub action2 :Global {
my ( $self, $c ) = @_;
$c->forward('action3');
}
sub action3 :Private {
my ( $self, $c ) = @_;
if ( ! $c->forward( 'user_modify_form' ) ) {
$c->res->body( to_json($c->form->field_messages('user_modify_form')) );
}
else {
$c->res->body('no errors');
}
}
sub action4 :Global {
my ($self, $c) = @_;
if ( ! $c->forward( 'user_modify_form2' ) ) {
$c->res->body( to_json($c->form->field_messages('user_modify_form2')) );
}
else {
$c->res->body('no errors');
}
}
sub action5 :Global {
my ($self, $c) = @_;
if ( ! $c->forward( 'user_descri_form' ) ) {
$c->res->body( to_json($c->form->field_messages('user_descri_form')) );
}
else {
$c->res->body('no errors');
}
}
sub user_regist_form :ActionClass('Validator') {
my ($self, $c) = @_;
# do more manual validation
}
sub user_modify_form :ActionClass('Validator') {}
sub user_modify_form2 :ActionClass('Validator') {
my ($self, $c) = @_;
$c->set_invalid_form( user_name => 'MY_ORIGINAL' );
}
sub user_descri_form :ActionClass('Validator') {}
}
use Catalyst::Test 'TestApp';
use HTTP::Request::Common;
sub validate {
my $url = $_;
my $res = request($url) or die "request to $url failed";
return $res->content;
}
run_is input => 'expected';
__END__
=== NOT_BLANK
--- input validate
/action1
--- expected chomp
{"user_name":["NOT_BLANK!"]}
=== NOT_BLANK and ASCII
--- input validate
/action1?user_url=aaa bbb
--- expected chomp
{"user_url":["ASCII!"],"user_name":["NOT_BLANK!"]}
=== NOT_BLANK and ASCII ok
--- input validate
/action1?user_name=aaa
--- expected chomp
no errors
=== ok after forward
--- input validate
/action2?user_name=aaa
--- expected chomp
no errors
=== original validator
--- input validate
/action4?user_name=aaa
--- expected chomp
{"user_name":["MY_ORIGINAL!"]}
=== plural arguments validator ng
--- input validate
/action5?user_description=12345678901
--- expected chomp
{"user_description":["TOO LONG!"]}
=== plural arguments validator
--- input validate
/action5?user_description=1234567890
--- expected chomp
no errors
package Catalyst::Action::Validator;
use strict;
use warnings;
use base 'Catalyst::Action';
use MRO::Compat;
#use Data::Dumper;
sub execute {
my $self = shift;
my ($controller, $c) = @_;
$self->next::method( @_ );
my $form = $c->stack->[-1]->{name};
my $config = $c->config->{validator};
my %rules = map {
my $name = $_;
$name => [
map {
$_->{rule};
} grep {
my $rule = ( ref $_->{rule} eq 'ARRAY' ) ? $_->{rule}->[0] : $_->{rule};
# NOT_BLANK is handled specially in FormValidator::Simple::Validator
$rule && ($rule eq 'NOT_BLANK' || FormValidator::Simple::Validator->can($rule));
} @{ $config->{columns}{$name} }
]
} @{ $config->{forms}{$form} };
#$c->log->debug("[execute]rules: ".Dumper(\%rules));
#FormValidator::Simple->set_messages( {
# action1 => {
# name => {
# NOT_BLANK => 'input name!',
# LENGTH => 'input name (length should be between 0 and 10)!',
# },
# email => {
# DEFAULT => 'input correct email address!',
# },
# },
#} );
my %messages = map {
my $name = $_;
my %ret = map {
my $rule = (ref ($_->{rule}) eq 'ARRAY') ? $_->{rule}->[0] : $_->{rule} || $_->{self_rule};
$rule => $_->{message};
} @{ $config->{columns}{$name} };
{
$name => \%ret;
};
} @{ $config->{forms}{$form} };
FormValidator::Simple->set_messages({ $form => \%messages });
#$c->log->debug("[execute]messages: ".Dumper(\%messages));
return $c->form( %rules )->success;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment