Skip to content

Commit

Permalink
added constraint MATCH for flexibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomill committed Jan 18, 2011
1 parent c0393a8 commit 3e50418
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/FormValidator/Lite/Constraint/Default.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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
13 changes: 12 additions & 1 deletion t/020_constraints/001_default.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Test::Base;
use FormValidator::Lite;
use CGI;

plan tests => 28;
plan tests => 29;

filters {
query => [qw/eval/],
Expand Down Expand Up @@ -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.