Skip to content

Instantly share code, notes, and snippets.

@hayajo
Last active December 21, 2015 10:19
Show Gist options
  • Save hayajo/6291004 to your computer and use it in GitHub Desktop.
Save hayajo/6291004 to your computer and use it in GitHub Desktop.
FormValidator::Liteで配列の制約
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;
@hayajo
Copy link
Author

hayajo commented Aug 21, 2013

こんなかんじ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment