Fellow Monasterians:

This might be splitting hairs, but I do so much of this type of thing as a Web developer, that I thought I should try to refine my methods.

For years I have used CGI::Application, HTML::Template, and the C::A plugin for HTML::FillInForm to handle form validation and error handling using a mixture of the H::T associate option and H::FIF:

[HTML] <p>Name: <input name="address" type="text" value="<tmpl_var address>" +/></p> <p>More info: <input name="more_info" type="checkbox" value="yes" /></ +p> [Perl] #note: %sql is later used to populate a database and notify e-mail #validate() untaints and checks for empty or invalid values my ( %sql, @errors, $error, $fifvalues ); ($sql{'address'}, $error ) = $self->validate( $self->query->param('add +ress') ); if ( $error ) { push @errors, ( { 'error' => 'Address'.$error } ); +} ($sql{'more_info'}, $error ) = $self->validate($self->query->param( 'm +ore_info') ); if ( $error ) { push @errors, ( { 'error' => 'More info'.$error } ) +; } $fifvalues->{'more_info'} = $sql{'more_info'}; #needed because 'associate' doesn't handle checkboxes, radios, and +selects my $template = $self->load_tmpl( 'contact.tmpl', associate => $self->query(); die_on_bad_params => 0, ); $template->param( errors => \@errors ); return $self->fill_form( \$template->output, $fifvalues );

But I got to thinking (always dangerous), why don't I use H::FIF only and drop extra code like <tmpl_var foobar> in my HTML and use just H::FIF to re-populate? So, I end up with:

[HTML] <p>Name: <input name="address" type="text" value="" /></p> <p>More info: <input name="more_info" type="checkbox" value="yes" /></ +p> [Perl] my ( %sql, @errors, $error, $fifvalues ); ($sql{'address'}, $error ) = $self->validate( $self->query->param('add +ress') ); if ( $error ) { push @errors, ( { 'error' => 'Address'.$error } ); +} ($sql{'more_info'}, $error ) = $self->validate($self->query->param( 'm +ore_info') ); if ( $error ) { push @errors, ( { 'error' => 'More info'.$error } ) +; } my $template = $self->load_tmpl( 'contact.tmpl', die_on_bad_params => 0, ); $template->param( errors => \@errors ); for my $key ( keys %sql ) { $fifvalues->{$key} = $sql{$key}; } return $self->fill_form( \$template->output, $fifvalues );

The question I've been asking myself is what do I gain or loose in style, method, efficiency, etc? IMHO, the later method of just using H::FIF to re-populate the form is cleaner than mixing the associate option with H::FIF.

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

In reply to Populating forms using HTML::FillInForm vs HTML::Template by bradcathey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.