Subject: | Hidden field bug |
Hidden fields is broken: it acts differently from available
documentation. It is not covered in the pod, which is a big problem,
so I went to the CGI::FormBuilder pod as recommended.
The docs there (search for "hidden") are also confusing but at any
rate, it says to do<PRE>
$form->keepextras(1);
$form->cgi_param(name => 'custid', value => $custid + 0);
</PRE>
However this does not work and instead generates:<PRE>
<div id="a_package_add_extra"><input id="name" name="name"
type="hidden" value="custid" />
<input id="name" name="name" type="hidden" value="value" />
<input id="name" name="name" type="hidden" value="242" /></div>
</PRE>
However this will work:<PRE>
$form->keepextras(1); # use hidden fields too
$form->cgi_param('custid', $custid + 0); # add hidden form field
</PRE>
Functionally I think the way hidden fields are treated is a bug.
You should be able to do so and set the type to be "hidden".
Likewise, being unable to read it with $form->field when in the action
subroutine is not good, though documented.
(i.e., You can't say $custid = $c->request->parameters->{$custid}; )
Also the use of the "static" method (which I have not tried but expect
removes the div around the hidden input element) ought to be covered
since people will wonder where that div came from.
Besides confusion about path args, GET form vars, and hidden form
vars, I think Catalyst suffers from a lack of real world sample code.
So I'm willing to offer the following code in case you wish to use all
or part of it. (Of course if you think I'm using anything wrong please
tell me so).
Thanks!
Matt Rosin
<PRE>
=head2 addpackage
Create a new package for a customer given the customer id.
=cut
sub addpackage : Local Form('admin/a_package_add') {
my ( $self, $c, $custid ) = @_;
$c->stash->{msg} = '';
my $form = $self->formbuilder;
unless (defined $custid) {
&showerr($c,"ERROR! Customer ID required."); return;
}
my $cust = $c->model('CatMgrDb::Customer')->find($custid);
unless (defined $cust) {
&showerr($c,"ERROR! Could not find customer id $custid."); return;
}
$form->action('/admin/addpackage_action/');
$form->keepextras(1); # use hidden fields too
$form->cgi_param('custid', $custid + 0); # add hidden form field
$c->stash->{template} = "admin/admin_portal_addpackage.tt2";
}
=head2 addpackage_action
Action for the addpackage form. Saves the info, making a new package
optionally with a new listing for it.
=cut
sub addpackage_action : Local Form('admin/a_package_add') {
my ( $self, $c, $listingid ) = @_;
my $form = $self->formbuilder;
$c->stash->{msg} = '';
# get customer
my $custid = 0;
$form->keepextras(1);
unless (defined $form->cgi_param('custid')) {
&showerr($c,"ERROR! Custid not in form param"); return;
}
$custid = 0 + $form->cgi_param('custid');
my $cust = $c->model("CatMgrDB::Customer")->find($custid);
unless ($cust) {
&showerr($c,"ERROR! Could not find customer id $custid."); return;
}
if ( $form->submitted ) {
if ( $form->validate ) { # Save form input into database
# make package
my $pkg = $c->model("CatMgrDB::Package")
->create({
statusmsg => 'preparing',
product => $form->field('products') + 0,
customer => $custid,
adminmemo => $form->field('adminmemo'),
available => 1,
});
my $pkgok = $pkg->update;
unless ($pkgok) {
showerr($c,"Failed to create new package record for customer
$custid" . "! $!"); return;
}
$c->stash->{msg} .= "Created package id " . $pkg->id;
# After saving, redisplay package list for this customer
$c->forward('/admin/customer_listpackages/' . $custid);
}
}
}
</PRE>