NAME
Validator::Custom::HTMLForm - HTML Form validator
Version
Version 0.0601
STATE
Validator::Custom::HTMLForm is not stable.
SYNOPSIS
use Validator::Custom::HTMLForm;
# Data
my $data = {
name => 'ABCD',
age => 29,
mail1 => 'name@gmail.com',
mail2 => 'name@gmail.com',
year => 2005,
month => 11,
day => 27,
}
# Validation rule
my $rule = [
name => [
'not_blank',
'ascii',
{length => [1, 30]}
],
age => [
'not_blank',
'int'
],
mail1 => [
'trim',
'not_blank',
'email_loose'
],
mail2 => [
'not_blank',
'email_loose'
],
[qw/mail1 mail2/] => [
'duplication'
],
{ date => ['year', 'month', 'day'] } => [
'date'
]
]
# Create validator object
my $vc = Validator::Custom::HTMLForm->new;
# Validate
my $result = $vc->validate($data, $rule);
METHODS
This module is Validator::Custom subclass. All methods of Validator::Custom is available.
Constraint functions
Validator::Custom::Trim all constraint functions are available
- defined
-
check if the data is defined.
- undefined
-
check if the data is undefined.
- not_blank
-
check if the data is not blank.
- blank
-
check if the is blank.
- not_space
-
check if the data do not containe space.
- int
-
check if the data is integer.
# valid data 123 -134
- uint
-
check if the data is unsigned integer.
# valid data 123
- decimal
-
my $data = { num => '123.45678' }; my $rule => [ num => [ {'decimal' => [3, 5]} ] ]; Validator::Custom::HTMLForm->new->validate($data,$rule);
each numbers (3,5) mean maximum digits before/after '.'
- ascii
-
check is the data consists of only ascii code.
- length
-
check the length of the data.
The following sample check if the length of the data is 4 or not.
my $data = { str => 'aaaa' }; my $rule => [ num => [ {'length' => 4} ] ];
when you set two arguments, it checks if the length of data is in the range between 4 and 10.
my $data = { str => 'aaaa' }; my $rule => [ num => [ {'length' => [4, 10]} ] ];
- http_url
-
verify it is a http(s)-url
my $data = { url => 'http://somehost.com' }; my $rule => [ url => [ 'http_url' ] ];
- selected_at_least
-
verify the quantity of selected parameters is counted over allowed minimum.
<input type="checkbox" name="hobby" value="music" /> Music <input type="checkbox" name="hobby" value="movie" /> Movie <input type="checkbox" name="hobby" value="game" /> Game my $data = {hobby => ['music', 'movie' ]}; my $rule => [ hobby => [ {selected_at_least => 1} ] ];
- regex
-
check with regular expression.
my $data = {str => 'aaa'}; my $rule => [ str => [ {regex => qr/a{3}/} ] ];
- duplication
-
check if the two data are same or not.
my $data = {mail1 => 'a@somehost.com', mail2 => 'a@somehost.com'}; my $rule => [ [qw/mail1 mail2/] => [ 'duplication' ] ];
-
check with Email::Valid.
my $data = {mail => 'a@somehost.com'}; my $rule => [ mail => [ 'email' ] ];
- email_mx
-
check with Email::Valid, including mx check.
my $data = {mail => 'a@somehost.com'}; my $rule => [ mail => [ 'email_mx' ] ];
- email_loose
-
check with Email::Valid::Loose.
my $data = {mail => 'a.@somehost.com'}; my $rule => [ mail => [ 'email_loose' ] ];
- email_loose_mx
-
my $data = {mail => 'a.@somehost.com'}; my $rule => [ mail => [ 'email_loose' ] ];
- date
-
check with Date::Calc
my $data = {year => '2009', month => '12', day => '13'}; my $rule => [ {date => [qw/year month day/]} => [ 'date' ] ]; $result->products->{date}; # 2009-12-13 00:00:00
You can specify options
# Convert DateTime object my $rule => [ {date => [qw/year month day/]} => [ {'date' => {'datetime_class' => 'DateTime', time_zone => 'Asia/Tokyo'}} ] ]; $result->products->{date}; # DateTime object # Convert Time::Piece object my $rule => [ {date => [qw/year month day/]} => [ {'date' => {'datetime_class' => 'Time::Piece'}} ] ]; $result->products->{date}; # Time::Piece object
- time
-
check with Date::Calc
my $data = {hour => '12', minute => '40', second => '13'}; my $rule => [ [qw/hour minute second/] => [ 'time' ] ];
- datetime
-
check with Date::Calc
my $data = { year => '2009', month => '12', day => '13' hour => '12', minute => '40', second => '13' }; my $rule => [ {datetime => [qw/year month day hour minute second/]} => [ 'datetime' ] ]; $result->products->{datetime}; # 2009-12-13 12:40:13
You can specify options
# Convert DateTime object my $rule => [ {datetime => [qw/year month day hour minute second/]} => [ {'datetime' => {'datetime_class' => 'DateTime', time_zone => 'Asia/Tokyo'}} ] ]; $result->products->{date}; # DateTime object # Convert Time::Piece object my $rule => [ {datetime => [qw/year month day hour minute second/]} => [ {'datetime' => {'datetime_class' => 'Time::Piece'}} ] ]; $result->products->{date}; # Time::Piece object
- datetime_strptime
-
check with DateTime::Format::Strptime.
my $data = {datetime => '2006-04-26T19:09:21+0900'}; my $rule => [ datetime => [ {'datetime_strptime' => '%Y-%m-%dT%T%z'} ] ]; $result->products->{datetime}; # DateTime object
- datetime_format
-
check with DateTime::Format::***. for example, DateTime::Format::HTTP, DateTime::Format::Mail, DateTime::Format::MySQL and etc.
my $data = {datetime => '2004-04-26 19:09:21'}; my $rule = [ datetime => [ {datetime_format => 'MySQL'} ] ];
- greater_than
-
numeric comparison
my $rule = [ age => [ {greater_than => 25} ] ];
- less_than
-
numeric comparison
my $rule = [ age => [ {less_than => 25} ] ];
- equal_to
-
numeric comparison
my $rule = [ age => [ {equal_to => 25} ] ];
- between
-
numeric comparison
my $rule = [ age => [ {between => [1, 20]} ] ];
- in_array
-
check if the food ordered is in menu
my $rule = [ food => [ {in_array => [qw/sushi bread apple/]} ] ];
AUTHOR
Yuki Kimoto, <kimoto.yuki at gmail.com>
SEE ALSO
Validator::Custom, Validator::Custom::Trim
FormValidator::Simple, Data::FormValidator
COPYRIGHT & LICENSE
Copyright 2009 Yuki Kimoto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.