开发者

How to make FormValidator::Simple to use the same rule for multiple keys?

Now it just looks very verbose:

key1 => THE_SAME_RULE,
key2 => THE_SAME_RULE,
..
keyn => THE_SAME_RULE,

Is there a way to express 开发者_Go百科this in more concise manner?


The values are just data so you could have an extra variable that holds the rule and then refer to that variable when building your rule set. For example:

my $email  = ['NOT_BLANK', 'EMAIL_LOOSE'];
my $result = FormValidator::Simple->check( $query => [
    mail1 => $email,
    mail2 => $email,
] );

In theory, you could build up a whole library of rules and then use that library everywhere:

my $result = FormValidator::Simple->check( $query => [
    mail  => $AskersRules::EMAIL,
    phone => $AskersRules::PHONE,
    # etc.
] );

Then you wouldn't have to repeat yourself and you'd have a central library for both re-use and testing purposes.

I think you can also do this:

{ ks => [ 'key1', 'key2' ] } => THE_SAME_RULE

if that's what you're after. From the fine manual:

my $result = FormValidator::Simple->check( $q => [
    { mails => ['mail1', 'mail2'] } => [ 'DUPLICATION' ],
] )


So you want a loop, and you want it inside an expression. That begs for map.

( map { $_ => THE_SAME_RULE } qw( key1 key2 .. keyn ) )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜