Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to verify that the user has filled out all the fields on a form. I keep getting an error saying that they are missing a field even when they are not. My output prints all the variables and then says the same field is always missing.
<dl compact> <dt><b>ChangeButton</b> <dd>:<i>Change</i>:<br> <dt><b>Department</b> <dd>:<i>Information Systems</i>:<br> <dt><b>FName</b> <dd>:<i>Kelly</i>:<br> <dt><b>HireDate</b> <dd>:<i>12/04/00</i>:<br> <dt><b>LName</b> <dd>:<i>Berry</i>:<br> <dt><b>Picture</b> <dd>:<i>03_02</i>:<br> <dt><b>RecID</b> <dd>:<i>5</i>:<br> <dt><b>Title</b> <dd>:<i>Web Developer</i>:<br> </dl> Content-type: text/html <html> <head> <title>Error: Missing field FName </title> </head> <body> <h1>Error: Missing field FName </h1>
Here is my current code.
foreach $field (qw(FName LName Department Title HireDate Picture RecID +)) { &CgiDie("Error: Missing field $field\n") unless defined $input->{$ +field}; }
Any help would be greatly appreciated. Prince99 "Too Much Is Never Enough"

Replies are listed 'Best First'.
Re: Form Verification
by Fastolfe (Vicar) on Jan 05, 2001 at 20:16 UTC
    I think the error lies in how you're generating $input, or perhaps in how it is scoped. Since we have absolutely no information to go on here, be sure you're not putting stuff in %input instead, for example, but calling $input as a hash reference. Be sure you're using strict and warnings (which would catch that). Come to us with more information about how that variable is built if you still have problems.

    You also probably want to test for "trueness" or the presence of a non-whitespace character ($whatever =~ /\S/) instead of defined if your goal is to prevent empty fields. If you want to see if $field is present in the list of keys in a %hash, you usually want exists instead of defined.

Re: Form Verification
by mrmick (Curate) on Jan 05, 2001 at 20:19 UTC
    You're failing on the first field in the list... As part of the debugging process, I would recommend you iterate through your list and print out the values of the fields to ensure that you are referencing them correctly.
    foreach $field (qw(FName LName Department Title HireDate Picture RecID +)) { print "$input->{$field}<BR>\n"; }

    Mick
Re: Form Verification
by Nimster (Sexton) on Jan 06, 2001 at 00:45 UTC
    It's easier than that. I had the same problem - apparently perl decides the param is defined even if it's empty. So simply use unless (param(whatever)) or sosuch. All it does is disable the possibility of '0' (no '''s) as an answer to the forum, but it verifies them alright (as they are "true")
    Hope it helps, cause your case seems a little different than mine.

    -Nimster
Re: Form Verification
by dkubb (Deacon) on Jan 15, 2001 at 16:25 UTC

    This may be over-kill for this problem, but there is a great CPAN module called HTML::FormValidator

    It allows some amazing CGI form validation, with predefined built-in data types, or the ability for you to register your own validation routines.

    A great thing about this library, is that you can predefine and reuse validation "objects" if you wish. Once you define an Order object, for example, you save the definition in a perl script. Then when you the HTML::FormValidator->new() constructor, you can pass the location of this pre-defined object. In theory, you'd never need to write the same validation routines for a given object type again.

    And best of all it encourages aggregation of all your validation code into libraries, for better code reuse