読者です 読者をやめる 読者になる 読者になる

FormValidator::Simple

perl cpan

はじめて使ってみた(正確にはラッパーは使ったことはあるけども)。最初の見た目がゴツかったが使ってみれば便利〜。

use FormValidator::Simple;

my $params = +{
    param1 => '12345',
    param2 => 'abcdefg',
    param3 => undef,
    mail1  => 'test@example.com',
    mail2  => 'test2@example.com',
    year   => 2010,
    month  => 2,
    day    => 30,
};

my $result = FormValidator::Simple->check( $params => [
    param1 => ['NOT_BLANK', 'ASCII'],
    param2 => ['NOT_BLANK', 'INT', ['LENGTH', 1, 3]],
    param3 => ['NOT_BLANK'],
    mail1  => ['EMAIL_LOOSE'],
    mail2  => ['EMAIL_LOOSE'],
    { mails => ['mail1', 'mail2'] } => ['DUPLICATION'],
    { date  => ['year', 'month', 'day'] } => ['DATE'],
] );

if ( $result->has_error ) {
    for my $key ($result->error) {
        my @types = map { $_ } $result->error($key);
        printf "$key:%s\n",join q{,}, @types;
    }
}
__END__
param2:INT,LENGTH
param3:NOT_BLANK
mails:DUPLICATION
date:DATE