martinjs has asked for the wisdom of the Perl Monks concerning the following question:

I am using CGI::Application and CGI::FormBuilder for a site I am developing. I have a runmode that brings up the initial form and that works fine. However, on submit it always returns to the default runmode page. I have tried specifiying the runmode directly, but that does not seem to be working for me. I need to go to a confirmation page (which could be a runmode itself or any other page that will work).

Any suggestions or links to some information about using CGI::Application and CGI::FormBuilder together would be appreciated.

Replies are listed 'Best First'.
Re: CGI Interaction Magic
by naikonta (Curate) on Jul 04, 2007 at 01:34 UTC
    Both modules are perfect combination to build "light" MVC-based application.

    You don't need to specify the runmode directly because CGI::FormBuilder can handle that for you if you set keepextras option to true. In this case, the runmode will be automatically put in a hidden field since you usually don't specify it in the form as user-filled in fields. When you submit the button, your CGI::Application-based program will recognize the runmode sent by the form. If you have to set it manually, just use the same option, or the corresponding method. You also have to initiate the value for the CGI parameter of the runmode (default is rm).

    use strict; use warnings; use base 'CGI::Application'; use CGI::FormBuilder; main->new->run; # emulated for example purpose sub setup { my $self = shift; $self->start_mode('create_form'); $self->run_modes([qw(create_form list)]); } sub create_form { my $self = shift; my @fields = qw(name age); my $cgi = $self->query; my $form = CGI::FormBuilder->new( fields => \@fields, params => $cgi, keepextras => 1, ); # or, instead of using the keepextras option # you call the method with the same name # $form->keepextras(1); # push onto the CGI parameter stack $cgi->param($self->mode_param => $self->get_current_runmode); # UPDATE: change hardcoded "rm" to method call print $form->submitted && $form->validated ? $form->confirm : $form->render; }
    If called without any argument, as in the browser if the URL is called without any parameter, then the runmode will be set to the start_mode which is create_form.
    $ perl cfb.pl Content-Type: text/html; charset=ISO-8859-1 <!-- Generated by CGI::FormBuilder v3.03 available from www.formbuilde +r.org --> <form action="cfb.pl" method="get"> <div> <input id="_submitted" name="_submitted" type="hidden" value="1" /><in +put id="rm" name="rm" type="hidden" value="create_form" /> <table> <tr valign="top"> <td>Name</td> <td><input id="name" name="name" type="text" /></td> </tr> <tr valign="top"> <td>Age</td> <td><input id="age" name="age" type="text" /></td> </tr> <tr valign="top"> <td align="center" colspan="2"><input id="_submit" name="_submit" ty +pe="submit" value="Submit" /></td> </tr> </table> </div> </form>

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: CGI Interaction Magic
by eric256 (Parson) on Jul 03, 2007 at 22:44 UTC

    Could you post what you have tried? How is your CGI::Application configured and how are you sending it the run mode?


    ___________
    Eric Hodges
Re: CGI Interaction Magic
by leocharre (Priest) on Jul 04, 2007 at 13:22 UTC

    I understand your pain. When I started using CGI::Application, I thought the whole thing was a big messy sham.
    Today, coding a webapp without it seems idotic to me- personally.

    Initially I was under the misunderstanding that the module was smarter then it really was. That somehow it magically kept track of what the next runmode was.

    As for myself, all my runmodes do one of two things when they end, they either

    • spit out a complete web page and offer the client a set of choices for 'next' runmode..
    • Or, they redirect or they forward to another runmode explicitly.

    As for the form..

    Check that your form is sending the next runmode as argument.

    This form is telling that the next runmode is 'doit'

    <form method="POST"> <input type="text" name="last_name" value=""> <input type="hidden" name="rm" value="doit"> <input type="submit" value="submit"> </form>

    With the following, I've had problems in different browser clients:

    <form action="?rm=doit"> <input type="text" name="last_name" value=""> <input type="submit" value="submit"> </form>

    This example does not tell the web app what the next runmode is

    <form> <input type="text" name="last_name" value=""> <input type="submit" value="submit"> </form>