Jason Purdy wrote:
> 
> ------------------------------------
> Example Hash:
> %requiredFields = (
>     'upload_file'                   => 'Document Source',
>     'doc_url||docfile'            => 'Specify either Document URL or Document File',
>     'capture_date'               => 'Capture Date' );
> 
> ------------------------------------
> Example Usage:
> #!/usr/local/bin/perl
> 
> use CGI;
> require 'rfields.pl';
> 
> $query = new CGI;
> 
> # let's start by checking the required fields
> checkRequiredFields($query, %requiredFields);
> 
> ...
> 
> ------------------------------------
> Code:
> sub checkRequiredFields {
>     my ($formHandle, %reqdFields) = @_;
>     my ($incomplete) = 0;
>     my (@incFields) = ();
> 
>     foreach $field (keys %reqdFields) {
>         if ($field =~ /(.*)\|\|(.*)/) {
>             $incomplete = 1 && push (@incFields, $reqdFields{$field})
>                 if ( (!$formHandle->param($1) || $formHandle->param($1) eq '') &&
>                      (!$formHandle->param($2) || $formHandle->param($2) eq '') );
>         } else {
>             $incomplete = 1 && push (@incFields, $reqdFields{$field})
>                 if !$formHandle->param($field) || $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
>     }
> }

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.

Reply via email to