FormValidator::LazyWay で検証ルールをまとめよう

2,834 views
2,744 views

Published on

CPAN モジュールの FormValidator::LazyWay について

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,834
On SlideShare
0
From Embeds
0
Number of Embeds
458
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

FormValidator::LazyWay で検証ルールをまとめよう

  1. 1. my $config = { rules => [ 'Email', 'String' ], setting => { strict => { email => { rule => ['Email#email'] }, password => { rule => [ { 'String#length' => { 'min' => '4', 'max' => '12' } }, 'String#ascii' ] }, } }, lang => 'ja', labels => { ja => { email => ' ', password => ' ' } }, };
  2. 2. use FormValidator::LazyWay; my $fv = FormValidator::LazyWay->new($config); my $result = $fv->check($q, { required => [qw/nickname email password/], optional => [qw/message/] });
  3. 3. my $result = $fv->check($q, { required => [qw/title/], optional => [qw/body/] }); my $result = $fv->check($q, { optional => [qw/title body/] });
  4. 4. my $result = $fv->check($q, { required => [qw/nickname email password/], optional => [qw/message/] }); if ( $result->has_error ) { print Dumper $result->error_message; # output #$VAR1 = { # 'email' => ' ', # 'password' => ' 4 12 ' #}; } else { # OK! print Dumper $result->valid; }
  5. 5. setting: my $result = $fv->check( $cgi, strict: { required => [qw/email/], email: level => { email => 'loose' rule: } - Email#email } loose: ); email: rule: - Email#email_loose
  6. 6. setting: regexp_map: '_id$': rule: - Number#integer
  7. 7. merge: date: format: "%04d-%02d-%02d" fields: - year - month - day strict: date: rule: - DateTime#date
  8. 8. setting: strict: date: filter: - Unify#hyphen rule: - DateTime#date fix: - DateTime#format: - '%Y-%m-%d'
  9. 9. my $result = $fv->check($q, { required => [qw/nickname email password/], optional => [qw/message/] }); if ( $result->has_error ) { print Dumper $res->error_message; } # DateTime print $result->valid->{date};
  10. 10. my $result = $fv->check($q, { required => [qw/nickname email password/], optional => [qw/message/] }); $result->custom_invalid( 'email_not_unique', ' ' ); if ( $res->has_error ) { print Dumper $res->error_message; }
  11. 11. my $wants_to_validate = { nickname => 'vkgtaro', email => 'vkg.taro@gmail.com', password => 'mypass', }; my $result = $fv->check($q, { required => [qw/nickname email password/], optional => [qw/message/], custom_parameters => $wants_to_validate, });
  12. 12. messages : ja : rule_message : __field__ __rule__ rule : Email#email : String#length : $_[min] $_[max]
  13. 13. messages : ja : rule_message : __field__ __rule__ rule : Email#email : String#length : $_[min] $_[max]

×