CGI::Validate 2.000 review

Download
by rbytes.net on

CGI::Validate is an advanced CGI form parser and type validation. SYNOPSIS use CGI::Validate; # GetFormData()

License: Perl Artistic License
File size: 9K
Developer: Byron Brummer
0 stars award from rbytes.net

CGI::Validate is an advanced CGI form parser and type validation.

SYNOPSIS

use CGI::Validate; # GetFormData() only
use CGI::Validate qw(:standard); # Normal use
use CGI::Validate qw(:subs); # Just functions
use CGI::Validate qw(:vars); # Just exception vars

## If you don't want it to check that every requested
## element arrived you can use this. But I don't recommend it
## for most users.
$CGI::Validate::Complete = 0;

## If you don't care that some fields in the form don't
## actually match what you asked for. -I don't recommend
## this unless you REALLY know what you're doing because this
## normally meens you've got typo's in your HTML and we can't
## catch them if you set this.
## $CGI::Validate::IgnoreNonMatchingFields = 1;

my $FieldOne = 'Default String';
my $FieldTwo = 8;
my $FieldThree = 'some default string';
my @FieldFour = (); ## For multi-select field
my @FieldFive = (); ## Ditto
my $EmailAddress= '';

## Try...
my $Query = GetFormData (
'FieldOne=s' => $FieldOne, ## Required string
'FieldTwo=i' => $FieldTwo, ## Required int
'FieldThree' => $FieldThree, ## Auto converted to the ":s" type
'FieldFour=s' => @FieldFour, ## Multi-select field of strings
'FieldFive=f' => @FieldFive, ## Multi-select field of floats
'Email=e' => $EmailAddress, ## Must 'look' like an email address
) or do {
## Catch... (wouldn't you just love a case statement here?)
if (%Missing) {
die "Missing form elements: " . join (' ', keys %Missing);
} elsif (%Invalid) {
die "Invalid form elements: " . join (' ', keys %Invalid);
} elsif (%Blank) {
die "Blank form elements: " . join (' ', keys %Blank);
} elsif (%InvalidType) {
die "Invalid data types for fields: " . join (' ', keys %InvalidType);
} else {
die "GetFormData() exception: $CGI::Validate::Error";
}
};

## If you only want to check the form data, but don't want to
## have CGI::Validate set anything use this. -You still have full
## access to the data via the normal B object that is returned.

use CGI::Validate qw(CheckFormData); # not exported by default
my $Query = CheckFormData (
'FieldOne=s', 'FieldTwo=i', 'FieldThree', 'FieldFour',
'FieldFive', 'Email',
) or do {
... Same exceptions available as GetFormData above ...
};

## Need some of your own validation code to be used? Here is how you do it.
addExtensions (
myType => sub { $_[0] =~ /test/ },
fooBar => &fooBar,
i_modify_the_actual_data => sub {
if ($_[0] =~ /test/) { ## data validation
$_[0] = 'whatever'; ## modify the data by alias
return 1;
} else {
return 0;
}
},
);
my $Query = GetFormData (
'foo=xmyType' => $foo,
'bar=xfooBar' => $bar,
'cat=xi_modify_the_actual_data' => $cat,
);


## Builtin data type checks available are:
s string # Any non-zero length value
w word # Must have at least one w char
i integer # Integer value
f float # Float value
e email # Must match m/^s*

CGI::Validate 2.000 search tags