NAME

Validator::Custom::HTMLForm - HTML Form validator based on Validator::Custom

Version

Version 0.0502

Caution

This module is yet experimental stage.

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 $validation_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, $validation_rule);

# Get invalid key
my @invalid_keys = $result->invalid_keys;

# Get converted product
my $products = $result->products;

# Validation rule and error messages
my $validation_rule = [
    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 is Validator::Custom subclass.

See also Validator::Custom.

and Validator::Custom::Trim constraint function is available.

See also Validator::Custom::Trim.

Constraint functions

defined

check if the data is defined.

not_blank

check if the data is not 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 $validation_rule => [
    num => [
        {'decimal' => [3, 5]}
    ]
];

Validator::Custom::HTMLForm->new->validate($data,$validation_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 $validation_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 $validation_rule => [
    num => [
        {'length' => [4, 10]}
    ]
];
http_url

verify it is a http(s)-url

my $data = { url => 'http://somehost.com' };
my $validation_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 $validation_rule => [
    hobby => [
        {selected_at_least => 1}
    ]
];
regex

check with regular expression.

my $data = {str => 'aaa'};
my $validation_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 $validation_rule => [
    [qw/mail1 mail2/] => [
        'duplication'
    ]
];
email

check with Email::Valid.

my $data = {mail => 'a@somehost.com'};
my $validation_rule => [
    mail => [
        'email'
    ]
];
email_mx

check with Email::Valid, including mx check.

my $data = {mail => 'a@somehost.com'};
my $validation_rule => [
    mail => [
        'email_mx'
    ]
];
email_loose

check with Email::Valid::Loose.

my $data = {mail => 'a.@somehost.com'};
my $validation_rule => [
    mail => [
        'email_loose'
    ]
];
email_loose_mx
my $data = {mail => 'a.@somehost.com'};
my $validation_rule => [
    mail => [
        'email_loose'
    ]
];
date

check with Date::Calc

my $data = {year => '2009', month => '12', day => '13'};
my $validation_rule => [
    {date => [qw/year month day/]} => [
        'date'
    ]
];

$result->products->{date}; # 2009-12-13 00:00:00

You can specify options

# Convert DateTime object
my $validation_rule => [
    {date => [qw/year month day/]} => [
        {'date' => {'datetime_class' => 'DateTime', time_zone => 'Asia/Tokyo'}}
    ]
];

$result->products->{date}; # DateTime object


# Convert Time::Piece object
my $validation_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 $validation_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 $validation_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 $validation_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 $validation_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 $validation_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 $validation_rule = [
    datetime => [
        {datetime_format => 'MySQL'}
    ]
];
greater_than

numeric comparison

my $validation_rule = [
    age => [
        {greater_than => 25}
    ]
];
less_than

numeric comparison

my $validation_rule = [
    age => [
        {less_than => 25}
    ]
];
equal_to

numeric comparison

my $validation_rule = [
    age => [
        {equal_to => 25}
    ]
];
between

numeric comparison

my $validation_rule = [
    age => [
        {between => [1, 20]}
    ]
];
in_array

check if the food ordered is in menu

my $validation_rule = [
    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:

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.