Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re: Re: CGI.pm Disillusionment

by cees (Curate)
on Jun 05, 2003 at 03:03 UTC ( [id://263210]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: CGI.pm Disillusionment
in thread CGI.pm Disillusionment

Check out HTML::FillInForm for populating form fields without needing to use CGI.pm to generate the HTML. I use it very successfully with CGI::Application, HTML::Template and Data::FormValidator. A typical C::A runmode for me looks like the following:

sub edit_form { my $self = shift; my $q = $self->query(); # load the HTML::Template object my $template = $self->load_tmpl('FormTemplate.tmpl') || die "Can't find template FormTemplate.tmpl"; if ($q->param('first_name')) { # The user has filled in the form, so we # check the values and update the database # if everything is OK my %form_data = $q->Vars; my %form_profile = ( required => [qw(first_name last_name)], optional => [qw(email)], constraints => { email => 'email', }, filters => [ 'trim' ], ); my ($valid, $missing, $invalid, $unknown) = Data::FormValidator->v +alidate(\%form_data, \%form_profile); if (@$missing || @$invalid) { # There were problems with the form my @errors; push @errors, map { { 'missing_'.$_ => 1 } } @$missing; push @errors, map { { 'invalid_'.$_ => 1 } } @$invalid; $template->param(errors => \@errors); } else { # Everything looks good, so update the database eval { ### Do some database stuff here My::DB::User->create($valid); }; if ($@) { $template->param(errors => [ { failed_on_update => 1} ]); } else { # The database was updated successfully # so display a success page $template->param(database_updated => 1); } } } return $self->fillinform(\$template->output()); } sub fillinform { my $self = shift; my $html = shift; # ref to string of HTML my $fif = new HTML::FillInForm; return $fif->fill(scalarref => $html, fobject => $self->query); }

And the template would have the following fields in it:

<TMPL_INCLUDE NAME="../header.tmpl"> <h2>Registration Form</h2> <TMPL_IF errors> <h4 class="error_hdr">There was a problem processing your request</h4> <ul> <TMPL_LOOP errors> <li class="error"> <TMPL_IF invalid_email>The email address you entered does not look l +ike a valid email address</TMPL_IF> <TMPL_IF missing_firstname>You are required to provide your first na +me</TMPL_IF> <TMPL_IF missing_lastname>You are required to provide your last name +</TMPL_IF> <TMPL_IF failed_on_create>Error: Failed to create the new user in t +he database.</TMPL_IF> </li> </TMPL_LOOP> </ul> </TMPL_IF> First Name: <input type="text" name="first_name" size="32" maxlength=" +72"><br /> Last Name: <input type="text" name="last_name" size="32" maxlength=" +72"><br /> email: <input type="text" name="email" size="32" maxlength=" +255"><br /> <input type="submit" name="submit" value="Submit"><input type="reset" + name="reset" value="Reset"><br /> <TMPL_INCLUDE NAME="../footer.tmpl">

CGI::Application keeps my code structured nicely, HTML::Template allows me to separate design from code (including the content of error messages intended for the end-user), Data::FormValidator does most of the input field checking, and HTML::FillInForm gives me sticky form fields. And I usually top it off with Class::DBI for the database interface.

This turned out to be a bit longer than I intended, but hopefully someone finds it useful (or someone points out where I can improve things :).

Cheers

Replies are listed 'Best First'.
Re: Re: Re: Re: CGI.pm Disillusionment
by Cody Pendant (Prior) on Jun 05, 2003 at 03:54 UTC
    That looks very interesting, cees, a good way around the stickiness issues. Thanks for posting the code.
    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-20 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found