htmlfill not setting checkbox default

25 views
Skip to first unread message

Hollister

unread,
Oct 11, 2010, 12:36:35 PM10/11/10
to pylons-discuss
When rendering a checkbox with a value, the checked attribute it not
being set:

>>> from formencode import htmlfill
>>> htmlfill.render("""<input type="checkbox" name="cbox" value="1">""",{'cbox': True})
'<input type="checkbox" name="cbox" value="1">'

But when a value is not defined, the checked attribute is being set
correctly:

>>> htmlfill.render("""<input type="checkbox" name="cbox">""",{'cbox': True})
'<input type="checkbox" name="cbox" checked="checked">'

This is using FormEncode-1.2.3dev-py2.6.egg.

Thanks,

-Hollister

Piotr Kęplicz

unread,
Oct 11, 2010, 12:54:03 PM10/11/10
to pylons-...@googlegroups.com
Hollister, poniedziałek 11 października 2010 18:36:

> When rendering a checkbox with a value, the checked attribute it not
>
> being set:
> >>> from formencode import htmlfill
> >>> htmlfill.render("""<input type="checkbox" name="cbox"
> >>> value="1">""",{'cbox': True})
> '<input type="checkbox" name="cbox" value="1">'

I'd say it's correct. Input value is "1", not True.

>>> htmlfill.render("""<input type="checkbox" name="cbox" value="1">""",

{'cbox': 1})
'<input type="checkbox" name="cbox" value="1" checked="checked">'

.pk.

Hollister

unread,
Oct 11, 2010, 5:53:22 PM10/11/10
to pylons-discuss
Yes, I realize that, but I think it makes more sense to control the
checked state independent of the value. This is the way that it's done
in HTML, where there is a separate attribute for 'checked'. Having
direct access to this via htmlfill would be more convenient, and
consistent with the behavior when there is no value attribute present.

-Hollister

On Oct 11, 12:54 pm, Piotr Kęplicz <kepl...@cmc.pl> wrote:
> Hollister, poniedzia³ek 11 pa¼dziernika 2010 18:36:

Piotr Kęplicz

unread,
Oct 11, 2010, 6:27:41 PM10/11/10
to pylons-...@googlegroups.com
Hollister, poniedziałek 11 października 2010 23:53:

> Yes, I realize that, but I think it makes more sense to control the
> checked state independent of the value.

No, because you can have multiple checkboxes with the same name but different
values. The only way to distinguish them is by these values.

.pk.

Hollister

unread,
Oct 11, 2010, 7:44:57 PM10/11/10
to pylons-discuss
Yes, I see your point about multiple checkboxes.

I guess the issue I have is the interaction between
webhelpers.html.tags.checkbox initially generating a value attribute
(of 1) when none is specified, and htmlfill taking a boolean value and
trying to set the checked state. For almost all of my uses, I don't
use the value of the checkbox, rather I just want to know if it's
checked, and then populate a bool field in the DB. For these cases,
explicitly specifying a value = True (whether it's checked or not)
solves the issue. That just seems counterintuitive and somewhat
klunky...but form posts with checkboxes has always been that way.

Thanks for your thoughts.

-Hollister

Reply all
Reply to author
Forward
0 new messages