Permalink
Browse files

added constraint MATCH for flexibility.

  • Loading branch information...
tomill committed Jan 18, 2011
1 parent c0393a8 commit 3e504186f96dd37f16197e3240ea5298e32fcbb1
Showing with 31 additions and 1 deletion.
  1. +19 −0 lib/FormValidator/Lite/Constraint/Default.pm
  2. +12 −1 t/020_constraints/001_default.t
@@ -57,6 +57,13 @@ rule 'CHOICE' => sub {
return 0;
};
+rule 'MATCH' => sub {
+ my $callback = shift;
+ Carp::croak("missing \$callback") if ref $callback ne 'CODE';
+
+ $callback->($_);
+};
+
1;
__END__
@@ -144,5 +151,17 @@ Synonym of REGEX.
The parameter is one of choice or not.
+=item MATCH
+
+ use MyApp::Util qw/is_foo/;
+
+ $validator->check(
+ foo => [[MATCH => \&is_foo ]],
+ bar => [[MATCH => sub { $_[0] eq 'foo' } ]],
+ );
+
+Check parameter using callback. Callback takes parameter as first argument,
+should return true/false.
+
=back
@@ -5,7 +5,7 @@ use Test::Base;
use FormValidator::Lite;
use CGI;
-plan tests => 28;
+plan tests => 29;
filters {
query => [qw/eval/],
@@ -177,3 +177,14 @@ __END__
z2 => 1,
)
+=== MATCH
+--- query: { 'z1' => 'ba3', 'z2' => 'bao' }
+--- rule
+(
+ z1 => [[MATCH => sub { $_[0] eq 'ba3' } ]],
+)
+--- expected
+(
+ z1 => 0,
+)
+

0 comments on commit 3e50418

Please sign in to comment.