Bug#493662: simple checkbox value issues
Joey Hess
joeyh at debian.org
Mon Aug 4 00:54:14 UTC 2008
Package: libcgi-formbuilder-perl
Version: 3.05.01-6
Severity: normal
I want a simple checkbox (with just one option, either it's checked, or
not). I want the form to see whether I checked it, of course, and just re-display
it in the same state when submitted.
#!/usr/bin/perl
use CGI::FormBuilder;
use CGI;
my $q=CGI->new;
print $q->header;
my $form=CGI::FormBuilder->new(sticky => 1, fields => ["testing"]);
print "got: ".$form->field("testing")."\n";
$form->field(
name => "testing",
type => "checkbox",
value => 1,
cgi => $q,
options => [ [ 1 => "checked?" ] ],
);
print $form->render;
If you try this code out, the checkbox will initially be checked, and it will say
"got:" above it. Leave it checked and post the form, and it stays checked, and it
shows "got: 1". As expected so far.
Now, uncheck it, and post the form. Here CGI::FormBuilder loses track of the checkbox
not being checked, and becomes checked again. This despite sticky mode being enabled.
This doesn't happen for other types of fields. So if it's converted to radio
buttons, it will remember if 0 or 1 was chosen, stickily:
#!/usr/bin/perl
use CGI::FormBuilder;
use CGI;
my $q=CGI->new;
print $q->header;
my $form=CGI::FormBuilder->new(sticky => 1, fields => ["testing"]);
print "got: ".$form->field("testing")."\n";
$form->field(
name => "testing",
type => "radio",
value => 1,
cgi => $q,
options => [ 0, 1 ],
);
print $form->render;
But, that's not as usable as a checkbox, IMHO.
To work around this problem, you have to avoid setting a value for the checkbox
when the form is submitted:
#!/usr/bin/perl
use CGI::FormBuilder;
use CGI;
my $q=CGI->new;
print $q->header;
my $form=CGI::FormBuilder->new();
$form->field(
name => "testing",
type => "checkbox",
cgi => $q,
options => [ [ 1 => "checked?" ] ],
);
print "got: ".$form->field("testing")."\n";
if (! $form->submitted) {
$form->field(name => "testing", value => 1);
}
print $form->render;
Works ok, but is not very consistent with the rest of formbuilder's behavior
WRT sticky fields, and was not easy for me to come up with..
-- System Information:
Debian Release: lenny/sid
APT prefers unstable
APT policy: (500, 'unstable'), (500, 'stable')
Architecture: i386 (i686)
Kernel: Linux 2.6.26-1-686 (SMP w/1 CPU core)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash
Versions of packages libcgi-formbuilder-perl depends on:
ii perl 5.10.0-11.1 Larry Wall's Practical Extraction
Versions of packages libcgi-formbuilder-perl recommends:
ii libhtml-parser-perl 3.56-1+b1 A collection of modules that parse
Versions of packages libcgi-formbuilder-perl suggests:
ii libcgi-session-perl 4.35-1 Persistent session data in CGI app
ii libhtml-template-perl 2.9-1 HTML::Template : A module for usin
pn libtemplate-perl <none> (no description available)
pn libtext-template-perl <none> (no description available)
-- no debconf information
--
see shy jo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.alioth.debian.org/pipermail/pkg-perl-maintainers/attachments/20080803/f1a3ec95/attachment.pgp
More information about the pkg-perl-maintainers
mailing list