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!


In reply to Re: CGI Interaction Magic by naikonta
in thread CGI Interaction Magic by martinjs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.