Excellent points - Thanks for pointing them out! I'll check out your
tutorial soon (took a quick glance earlier this afternoon and it looks very
thorough, and a good read! Thanks! :)).
I forgot the print $query->header part of the code. I also updated the code
to accept '0' values by grepping within the keywords. I currently don't
have to worry about multivalued parameters - I don't design my forms that
way.
Jason
New subroutine:
sub checkRequiredFields {
my ($formHandle, %reqdFields) = @_;
my ($incomplete) = 0;
my (@incFields) = ();
foreach $field (keys %reqdFields) {
if ($field =~ /(.*)\|\|(.*)/) {
$incomplete = 1 && push (@incFields, $reqdFields{$field})
if ( (!grep(/$1\b/, $formHandle->keywords) ||
$formHandle->param($1) eq '') &&
(!grep(/$2\b/, $formHandle->keywords) ||
$formHandle->param($2) eq '') );
} else {
$incomplete = 1 && push (@incFields, $reqdFields{$field})
if !grep(/$field\b/, $formHandle->keywords) ||
$formHandle->param($field) eq '';
}
}
if ($incomplete) {
print "<H2>Incomplete Form</H2>\n";
print "You missed the required fields:<BR>\n<UL>\n";
foreach $incField (@incFields) { print "<LI>$incField\n"; }
print "</UL>\n";
print "<CENTER><I>Use your browser's back button & try
again</I></CENTER>\n";
exit; # I couldn't just use the die, b/c it wouldn't format the
$msg like I wanted
}
}
----- Original Message -----
From: "fliptop" <[EMAIL PROTECTED]>
To: "Jason Purdy" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Sunday, July 08, 2001 1:38 PM
Subject: Re: Required Fields Module
[snip]
>
> hi jason
>
> after looking at your code, and not typing it in and trying it out, here
> are some of the problems i see with it:
>
> problem: what if a user passes a value of 0 in one of the required
> fields?
>
> let's say one of your required fields is 'number_of_kids'. some people
> may not have any, and therefore may enter '0'. your code checks to see
> if a required field is true/false. your code will not accept a value of
> 0 (it will think it's false, even though it's a valid answer).
>
> instead of checking to see if a field is true/false, check to see if
> it's defined, then check to see if it contains a minimum number of
> characters.
>
> problem: if there is an error, you're not sending the appropriate header
> command.
>
> if you just try to print HTML without calling $query->header, the result
> will be an internal server error.
>
> problem: what if your parameters are multivalued?
>
> assume for the moment your cgi is called like this:
>
> /cgi-bin/whatever.cgi?name=fliptop&lang=perl&lang=english
>
> will your code handle these values the way you want?
>
> here are my recommendations:
>
> 1) put the parameters you're looking for into a list like this: (key1,
> value1, key2, value2, key2, value3, ...)
> 2) create a parameter hash from this list with values being either
> scalar or references to arrays:
> %parameters = (
> key1 => value1,
> key2 => [ value2, value3]
> );
> 3) check each key's value(s) (if it's required, ie.- not NULL) to see
> that they're defined and have a minimum length.
>
> if you haven't already seen it, i'd recommend reading the tutorial i'm
> working on which explains how to do all of these things. it can be
> found at http://www.peacecomputers.com/addressbook_toot-intro.html.