Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

(jeffa) Re: Generalising Forms from CGI.pm

by jeffa (Chancellor)
on Feb 19, 2003 at 02:01 UTC ( #236494=note: print w/ replies, xml ) Need Help??


in reply to Generalising Forms from CGI.pm

Hmmmm ... personally, i would use a templating system for this kind of problem, but ... if you don't mind the perfomance hit, you could point each key in your hash to an anonymous subroutine that takes a single arg and calls the proper CGI subroutine:

my $thisrow; my @rows; my %fields = ( name => sub { textfield(-name=>$_[0],-size=>50,-maxlength=>80 +) }, email => sub { textfield(-name=>$_[0],-size=>50,-maxlength=>80 +) }, site_name => sub { textfield(-name=>$_[0],-size=>50,-maxlength=>80 +) }, descripton => sub { textfield(-name=>$_[0],-size=>50,-maxlength=>80 +) }, comments => sub { textarea(-name=>$_[0],-cols=>50,-rows=>10) }, ); foreach my $key (keys %fields) { $thisrow = td({-align=>'right',-valign=>'TOP'},$key) . td({-align=>'left',-valign=>'TOP'}, $fields{$key}->($key)) ; push(@rows,$thisrow); }
However, i would never use that in any of my code. The way i see it, using CGI.pm HTML generation methods IS my abstraction. Wrapping that into something else seems a bit silly. Have you tried HTML::Template yet? You might find that it is all you need. ;)

p.s. don't forget that hashes are not ordered - you might want to try a list of hashes instead

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)


Comment on (jeffa) Re: Generalising Forms from CGI.pm
Download Code
Re: (jeffa) Re: Generalising Forms from CGI.pm
by clscott (Friar) on Feb 19, 2003 at 22:41 UTC

    I can't believe no one has mentioned CGI::Formbuilder yet:

    I believe that Nathan Wiger has scratched this itch particularly well, making it very straight forward to do complicated things with CGI forms very simply.

    For posperity here is the SYNOPSYS from the documentation

    use CGI::FormBuilder; # Let's assume we did a DBI query to get existing values my $dbval = $sth->fetchrow_hashref; my $form = CGI::FormBuilder->new( method => 'POST', fields => [qw/name email phone gender/], values => $dbval, validate => { email => 'EMAIL', phone => 'PHONE' } +, required => 'ALL', font => 'arial,helvetica', ); # Change gender field to have options $form->field(name => 'gender', options => [qw/Male Female/]); if ($form->submitted && $form->validate) { my $fields = $form->field; # get form fields as hashref # Do something to update your data (you would write this) do_data_update($fields->{name}, $fields->{email}, $fields->{phone}, $fields->{gender}); # Show confirmation screen print $form->confirm(header => 1); # Email the person a brief confirmation $form->mailconfirm(to => $fields->{email}); } else { # Print out the form print $form->render(header => 1); }
    --
    Clayton aka "Tex"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-10-26 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls