Last active
December 21, 2015 10:19
-
-
Save hayajo/6291004 to your computer and use it in GitHub Desktop.
FormValidator::Liteで配列の制約
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyApp::Validator::Constraint; | |
use strict; | |
use warnings; | |
use FormValidator::Lite::Constraint; | |
=head1 SYNOPSYS | |
my $fv = FormValidator::Lite->new($q); | |
$fv->check( | |
array1 => [qw/ARRAY/], | |
array2 => [qw/NOT_NULL/, [ ARRAY => 'UINT' ]], | |
array3 => [qw/NOT_NULL/, [ ARRAY => [ EQUAL => 'hoge' ] ]], | |
); | |
=cut | |
rule 'ARRAY' => sub { | |
my $target = $_; | |
return unless ( ref $target eq 'ARRAY' ); | |
my @rules = @_; | |
return 1 unless ( scalar(@rules) ); | |
for my $value (@$target) { | |
local $_ = $value; | |
for my $rule (@rules) { | |
my $rule_name = ref($rule) ? $rule->[0] : $rule; | |
my $rule_args = ref($rule) ? [ @$rule[ 1 .. scalar(@$rule)-1 ] ] : +[]; | |
my $code = $FormValidator::Lite::Rules->{$rule_name}; | |
return unless $code->(@$rule_args); | |
} | |
} | |
return 1; | |
}; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こんなかんじ?