I've been using Data::FormValidator and HTML::FillInForm with
CGI::Application as per a suggestion in an earlier thread on this list.  The
technique was demonstrated by Mike & Christine and Mark Stosberg (thread was
Re: [cgiapp] form validation back in July).  It works relatively well for
me.  This is roughly the code Mike & Christine sent to the list:

    my( $valid,$missing,$invalid,$unknown ) =
      $validator->validate(\%fdat,$self->param('profile_name'));
    if( @$invalid ) {
        $form_ok = 0;
        for(@$invalid){
            $q->param($_."_err",1);
        }
    }
    if( @$missing ) {
        $form_ok = 0;
        for(@$missing){
            $q->param($_."_miss",1);
        }
    }

For any required form parameter 'foo' that's missing, foo_miss gets setup.
If foo fails validation for another reason, foo_err gets setup.  By passing
"associate => $q" in as an option to load_tmpl, you get access to the _miss
and _err variables as TMPL_VARs.  By checking for them with TMPL_IF
statements, you can keep all your error messages and error message
formatting in your HTML files.

I created a class that inherits from CGI::Application and does this on a
generic basis.  In my .cgi driver I just set:
- the name of the template files to use (for the form and success pages)
- the name of the Data::FormValidator 'profile' file (and which profile
within the file to use)
- the email address of the person that should receive the form data (if
validation succeeds)
- the file name of the file to save form data to (if the form validation
succeeds)
- toggles for 'do_email' and 'do_save' and 'do_extra' settings ('do_extra'
calls an extra function that can be overriden)

One stumbling block I ran into was making the fields appear in a logical
order when they get sent as an email or saved in a file.  I have a feeling
my solution (passing additional 'field order' params in the .cgi is not the
best way to do it).

Like I said, the above method seems to work relatively well.  I like the
method Joel Gwynn presented though too.  With his method, it would be pretty
easy to handle the field order issue gracefully.

-Will

----- Original Message -----
From: "Joel Gwynn" <[EMAIL PROTECTED]>
To: "'John Lawton'" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Friday, March 01, 2002 2:01 PM
Subject: RE: [cgiapp] Form validation


> Well now, that reminds me of a funny story.  I remember like it was
> yesterday ...
>
> I had to code a survey with over 500 fields, with information being
> collected over 6 or so pages.  Usually, I would just store the required
> field names in a hash, and loop through the hash and validate.  In this
> case, I made a new table in the database, called form_fields.  I put a
> row in the table for each form field, along with the type of data, the
> page/runmode it was gathered by, a description (handy for error
> messages), the table into which it was to be inserted, and whether it
> was required.  Then for each page, I just called my function
> &validate($self), which selected all the rows matching the current
> runmode, and validated the data accordingly.
>
> There's probably a smarter way to do it, using the data dictionary or
> something, but it did the trick for me, and it's database-independent.
>
> HTH.
>
> -----Original Message-----
> From: John Lawton [mailto:[EMAIL PROTECTED]]
> Sent: Friday, March 01, 2002 1:28 PM
> To: [EMAIL PROTECTED]
> Subject: [cgiapp] Form validation
>
>
> While this may be peripheral to CGI::Application, I'd like to get some
> feedback on what others are using for form validation. I am particularly
>
> interested in documented (tested) packages, as opposed to homegrown, ad
> hoc solutions. (With all due respect to the hackers out there)
>
> I have taken a look at Data::FormValidator. This module makes it
> possible to describe the required/optional parameters, dependencies,
> filters, and constraints in a separate file (metafile).  I like the
> approach this module takes, from my experience as a programmer, it seems
>
> like this module is comprehensive. However, I am still wondering if
> CGI::Application people are using any other interesting modules for this
>
> purpose?
>
> -john
>
> PS I have also taken a cursory look at Params::Validate, it appears to
> be designed to solve a slightly different problem, parameter validation
> when passing parameters into methods.
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>
>
> ---------------------------------------------------------------------
> Web Archive:  http://www.mail-archive.com/[email protected]/
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
> ---------------------------------------------------------------------
> Web Archive:  http://www.mail-archive.com/[email protected]/
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[email protected]/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to