Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Automatic Generation of Form Handling Code

by Cirollo (Friar)
on Jun 06, 2001 at 21:43 UTC ( [id://86316]=note: print w/replies, xml ) Need Help??


in reply to Automatic Generation of Form Handling Code

Funny, I was contemplating something along those lines earlier this morning...

To populate variables based on the form data structure, CGI::State might be a good place to start; it loads CGI parameters into a multidimensional hash. Of course, you would have to change the form element names in your HTML to conform with the module.

  • Comment on Re: Automatic Generation of Form Handling Code

Replies are listed 'Best First'.
(Ovid) Re(2): Automatic Generation of Form Handling Code
by Ovid (Cardinal) on Jun 06, 2001 at 22:04 UTC

    CGI::State looks interesting. However, the time that it takes to go through all of the forms and update the field names might just negate the benefits. I can always get the form data in a hash like so:

    use CGI qw/:standard/; my %formdata = map { $_ => [ param( $_ ) ] } param;

    That's easy. However, I really dislike doing that as it makes it much easier to miss untainting/validating a particular variable (IMHO). Also, I explicitly like to see the checkbox groups represented as arrays and individual elements represented as scalars. It's more obvious to me how to handle them. Hmm... am I just being foolish? I guess I really can't see the difference between populating a bunch of scalars and populating a hash with all of the data aside from the fact that single value form elements are now represented as a one element array reference, removing the clear visual distinction between arrays and scalars:

    # The following is clearer for me: my $first_name = $in_name; foreach ( @in_colors ) { # do something } # This is less clear: my $first_name = $formdata{ 'in_name' }[0]; foreach ( @$formdata{ 'in_colors' } ) { # do something }

    Is that a matter of style over substance, or is this something that could actually be an issue (particularly with maintenance?).

    I'm beginning to think this should have been in meditations instead.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Lately, most of the code I write for CGI's generally follows the same basic design pattern:

      • Receive the values with CGI.pm.
      • Validate and detaint the parameters with HTML::FormValidator.
      • <shamless plug>Build a multi-dimensional hash with CGI::State</shameless plug>
      • If there is an error, or missing field, use HTML::FillInForm and HTML::Template to re-fill in the form with the submitted data and print an error.
      • Do some work, usually with DBI, etc
      • Use HTML::Template to display a page to the user either prompting the user for more info, or display the results of the work.

      (I hear that Apache::Pagekit encapsulates alot of this into a single framework, but I am not convinced that the platform is stable enough for my needs yet.)

      The great thing about HTML::FormValidator is that you can set up "validation profiles". By this I mean you can built a set of rules for a newsletter subscription, or an order submission, for example. The design of the module allows it to share these pre-made profiles with many other scripts. Once you write a profile for what I call a "web object", you can reuse it over and over for the same type of data.

      If your 40-odd scripts are asking for similar types of data, using this module might be a good way to factor out all the validation and detainting code.

      Update: Sorry, I see that you have 40 elements per form, not scripts. Either way, this module *will* save you enough time, so you can concentrate on Quake more =)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://86316]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-19 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found