Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

(jeffa) Re: Generalising Forms from

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

in reply to Generalising Forms from

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 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


(the triplet paradiddle with high-hat)

Comment on (jeffa) Re: Generalising Forms from
Download Code
Replies are listed 'Best First'.
Re: (jeffa) Re: Generalising Forms from
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?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2015-11-29 02:14 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (746 votes), past polls