Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added constraint MATCH for flexibility.

  • Loading branch information...
commit 3e504186f96dd37f16197e3240ea5298e32fcbb1 1 parent c0393a8
@tomill tomill authored
View
19 lib/FormValidator/Lite/Constraint/Default.pm
@@ -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
View
13 t/020_constraints/001_default.t
@@ -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,
+)
+
Please sign in to comment.
Something went wrong with that request. Please try again.