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.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |