FieldValidator(3)
NAME
WWW::FieldValidator - Provides simple yet robust valida
tion of user entered input
SYNOPSIS
OO module that is used to validate input.
DESCRIPTION
This module is used by WWW::Form to perform various vali
dations on input. This document covers using the
WWW::FieldValidator module as part of a Form object. In
this case, the only thing you need to know how to do is to
instantiate WWW::FieldValidators properly. All the vali
dation is handled internally by WWW::Form.
Function Reference
- new($validatorType, $errorFeedback, [$minLength, $maxLength, $regex], [$isOptional])
- Creates a FieldValidator object. $validatorType is used
- to determine what type
of validation will be performed on the input. The fol - lowing validator types are
supported (Note these are constants, the $validatorType - param needs to be one of
the following values): - WWW::FieldValidator::WELL_FORMED_EMAIL # input must con
- form to /^[216
WWW::FieldValidator::MIN_STR_LENGTH # input must be - >= a specified string length
WWW::FieldValidator::MAX_STR_LENGTH # input must be - <= a specified string length
WWW::FieldValidator::REGEX_MATCH # input must - match a user defined regex
WWW::FieldValidator::USER_DEFINED_SUB # input must pass - a user defined subroutine's validation
- Examples:
- # create a validator that checks to see if input is a
- well formed email address
WWW::FieldValidator->new(WWW::FieldValida - tor::WELL_FORMED_EMAIL,
'Please make sure you enter awell formed email address');
- # creates a validator that checks to see if input is
# well formed only if input is not null (or numm string)
WWW::FieldValidator->new(WWW::FieldValida - tor::WELL_FORMED_EMAIL,
- 'Please make sure you enter a
- well formed email address',
$isOptional = 1); - # creates a validator that checks to see if the input is
- at least min length
WWW::FieldValidator->new(WWW::FieldValida - tor::MIN_STR_LENGTH,
- 'Please make sure you enter
- something at least 10 characters long',
10); - # creates a validator that checks to see if the input is
- at least min length
# only if input is not null or null string
WWW::FieldValidator->new(WWW::FieldValida - tor::MIN_STR_LENGTH,
- 'Please make sure you enter
- something at least 10 characters long',
10,
1); - # creates a validator that checks to see if the input is
- less than max length
WWW::FieldValidator->new(WWW::FieldValida - tor::MAX_STR_LENGTH,
- 'Please make sure you enter
- something less than or equal to 5 characters',
5); - # creates a validator that checks to see if the input is
- less than max length
# only if input is not null or null string
WWW::FieldValidator->new(WWW::FieldValida - tor::MAX_STR_LENGTH,
- 'Please make sure you enter
- something less than or equal to 5 characters',
5,
1); - # creates a validator that checks to see if the input
- matches the specified regex
WWW::FieldValidator->new(WWW::FieldValida - tor::REGEX_MATCH,
- 'Please make sure you enter a
- number',
^+$|^+.*$|^*.+$'); - # creates a validator that checks to see if the input
- matches the specified regex
# only if input is not null or null string
WWW::FieldValidator->new(WWW::FieldValida - tor::REGEX_MATCH,
- 'If you are going to enter any
- thing please make sure you enter a number',
^+$|^+.*$|^*.+$',
1); - # creates a validator that checks to see if the input is
- good according to sub ref
WWW::FieldValidator->new(WWW::FieldValidator::USER_DE - FINED_SUB,
- 'The name you entered already
- exists',
is_name_unique); - # creates a validator that checks to see if the input is
- good according to sub ref
# only if input is not null or null string
WWW::FieldValidator->new(WWW::FieldValidator::USER_DE - FINED_SUB,
- 'If you are going to enter a
- name, you must enter one that does not already exist',
is_name_unique,
1); - # if you use the validator type: USER_DEFINED_SUB, your
- subroutine will have access to
# the value of the form input that your validator is as - signed to
Example:
sub is_name_unique { - # gets passed in to this sub for you by way of Form
- module
my $name = shift; - if ($names->{$name}) {
return 0; # name already exists, input is invalid
- } else {
return 1;
- }
- }
- If you want to use WWW::FieldValidator outside of
WWW::Form it's easy to do. The only method you need to
use is validate. - validate($input)
- Returns true if $input passes validation or false other
wise.
Example:- my $email_validator = WWW::FieldValida
- tor->new(WWW::FieldValidator::WELL_FORMED_EMAIL,
'Please make sure you enter awell formed email address');
- my $params = $r->param();
- if (my $email = $params->{email}) {
unless ($email_validator->validate($email)) {print $email_validator->getFeedback();- }
- }
- ggeettFFeeeeddbbaacckk(())
- Returns error feedback for a FieldValidator. This can
also be called as get_feedback().
SEE ALSO
WWW::Form
AUTHOR
Ben Schmaus
If you find this module useful or have any suggestions or
comments please send me an email at perlmods@ben
schmaus.com.
BUGS
None that I know of, but let me know if you find any.
Send email to perlmods@benschmaus.com
COPYRIGHT
Copyright 2003, Ben Schmaus. All Rights Reserved.
- This program is free software. You may copy or redis
tribute it under the same terms as Perl itself. If you
find this module useful, please let me know.