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).
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.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; }
$ 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |