NAME
Validator::Custom::HTMLForm - HTML Form validator based on Validator::Custom
VERSION
Version 0.0102
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,
}
# Validators
my $validators = [
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
$vc->validate($data, $validators);
# Get invalid key
my @invalid_keys = $vc->invalid_keys;
# Get converted result
my $results = $vc->results;
# Validators and error message
my $validators = [
name => [
['NOT_BLANK', 'name must be exist'],
['ASCII', 'name must be acsii']
[{LENGTH => [1, 30]}, 'name must be length 1 to 30']
],
age => [
['NOT_BLANK', 'age must be exist'],
['INT', 'age must be integer value']
],
]
# Get error message on one linear
my @errors = Validator::Custom::HTMLForm->new->validate($data,$validator)->errors;
DESCRIPTION
This module usage is same as Validator::Custom.
See Validator::Custom document.
VALIDATION COMMANDS
- SP
-
check if the data containe space.
- NOT_BLANK
-
check if the data is not blank.
- 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 $validators => [ num => [ {'DECIMAL' => [3, 5]} ] ]; Validator::Custom::HTMLForm->new->validate($data,$validators);
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 $validators => [ 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 $validators => [ num => [ {'LENGTH' => [4, 10]} ] ];
- HTTP_URL
-
verify it is a http(s)-url
my $data = { url => 'http://somehost.com' }; my $validators => [ 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 $validators => [ hobby => [ {SELECTED_AT_LEAST => 1} ] ];
- REGEX
-
check with regular expression.
my $data = {str => 'aaa'}; my $validators => [ 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 $validators => [ [qw/mail1 mail2/] => [ 'DUPLICATION' ] ];
-
check with Email::Valid.
my $data = {mail => 'a@somehost.com'}; my $validators => [ mail => [ 'EMAIL' ] ];
- EMAIL_MX
-
check with Email::Valid, including mx check.
my $data = {mail => 'a@somehost.com'}; my $validators => [ mail => [ 'EMAIL_MX' ] ];
- EMAIL_LOOSE
-
check with Email::Valid::Loose.
my $data = {mail => 'a.@somehost.com'}; my $validators => [ mail => [ 'EMAIL_LOOSE' ] ];
- EMAIL_LOOSE_MX
-
my $data = {mail => 'a.@somehost.com'}; my $validators => [ mail => [ 'EMAIL_LOOSE' ] ];
- DATE
-
check with Date::Calc
my $data = {year => '2009', month => '12', day => '13'}; my $validators => [ {date => [qw/year month day/]} => [ 'DATE' ] ]; $vc->results->{date}; # 2009-12-13 00:00:00
You can specify options
# Convert DateTime object my $validators => [ {date => [qw/year month day/]} => [ ['DATE', {'datetime_class' => 'DateTime', time_zone => 'Asia/Tokyo'}] ] ]; $vc->results->{date}; # DateTime object # Convert Time::Piece object my $validators => [ {date => [qw/year month day/]} => [ ['DATE', {'datetime_class' => 'Time::Piece'}] ] ]; $vc->results->{date}; # Time::Piece object
- TIME
-
check with Date::Calc
my $data = {hour => '12', minute => '40', second => '13'}; my $validators => [ [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 $validators => [ {datetime => [qw/year month day hour minute second/]} => [ 'DATETIME' ] ]; $vc->results->{datetime}; # 2009-12-13 12:40:13
You can specify options
# Convert DateTime object my $validators => [ {datetime => [qw/year month day hour minute second/]} => [ ['DATETIME', {'datetime_class' => 'DateTime', time_zone => 'Asia/Tokyo'}] ] ]; $vc->results->{date}; # DateTime object # Convert Time::Piece object my $validators => [ {datetime => [qw/year month day hour minute second/]} => [ ['DATETIME', {'datetime_class' => 'Time::Piece'}] ] ]; $vc->results->{date}; # Time::Piece object
- DATETIME_STRPTIME
-
check with DateTime::Format::Strptime.
my $data = {datetime => '2006-04-26T19:09:21+0900'}; my $validators => [ datetime => [ {'DATETIME' => '%Y-%m-%dT%T%z'} ] ]; $vc->results->{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 $validators = [ datetime => [ {DATETIME_FORMAT => 'MySQL'} ] ];
- GREATER_THAN
-
numeric comparison
my $validators = [ age => [ {GREATER_THAN => 25} ] ];
- LESS_THAN
-
numeric comparison
my $validators = [ age => [ {LESS_THAN => 25} ] ];
- EQUAL_TO
-
numeric comparison
my $validators = [ age => [ {EQUAL_TO => 25} ] ];
- BETWEEN
-
numeric comparison
my $validators = [ age => [ {BETWEEN => [1, 20]} ] ];
- IN_ARRAY
-
check if the food ordered is in menu
my $validators = [ food => [ {IN_ARRAY => [qw/sushi bread apple/]} ] ];
- TRIM
-
Trim leading and trailing white space
- TRIM_LEAD
-
Trim leading white space
- TRIM_TRAIL
-
Trim trailing white space
- TRIM_COLLAPSE
-
Trim leading and trailing white space, and collapse all whitespace characters into a single space.
AUTHOR
Yuki Kimoto, <kimoto.yuki at gmail.com>
BUGS
Please report any bugs or feature requests to bug-validator-custom-htmlform at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Validator-Custom-HTMLForm. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Validator::Custom::HTMLForm
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Custom-HTMLForm
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
SEE ALSO
Validator::Custom, Validator::Custom::Trim
FormValidator::Custom, 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.