form.py checkbox cannot have a value

Bug #128233 reported by keizo
2
Affects Status Importance Assigned to Milestone
web.py
Fix Released
Medium
Anand Chitipothu

Bug Description

It seems if you set any value in a checkbox, it renders as checked. So there's no way to have a value and not have it checked. This is troublesome when you want checkboxes of the same name to produce a list of values.

#i.e. I want to do something similar to this:
names = web.select('names')
checkboxes = []
for n in names:
    checkboxes.append(form.Checkbox('names', value=n.id,checked=n.status, description=n.name))

myform = form.Form(*tuple(checkboxes))

#in code.py
names = web.input(names=[]).names

I've changed the checkbox class as follows and I hope it or something similar can be committed:

class Checkbox(Input):
    def __init__(self, name, *validators, **attrs):
        self.checked = attrs.pop('checked', "")
        super(Checkbox, self).__init__(name, *validators, **attrs)

    def render(self):
        x = '<input name="%s" type="checkbox"' % net.websafe(self.name)
        if self.value: x += ' value="%s"' % net.websafe(self.value)
        if self.checked: x += ' checked="checked"'
        x += self.addatts()
        x += ' />'
        return x

Aaron Swartz (aaronsw)
Changed in webpy:
assignee: nobody → anandology
importance: Undecided → Medium
status: New → Confirmed
Revision history for this message
keizo (kzo) wrote :

I just started looking at my code after 8 months, so I am a little rusty, but I think the changes I stated in this bug report break the regular case for a checkbox. So maybe call it a new class -- CheckboxList -- similar to that other bug report for radio buttons.

Revision history for this message
Anand Chitipothu (anandology) wrote :

Fixed.

Changed in webpy:
status: Confirmed → Fix Committed
Revision history for this message
SeC (sec) wrote :

>>> web.form.Checkbox('checkbox1', value = 'test').render()
'<input type="checkbox" id="checkbox1" name="checkbox1"/>'

Still no 'value' attribute.

Revision history for this message
Anand Chitipothu (anandology) wrote : Re: [Bug 128233] Re: form.py checkbox cannot have a value

2009/9/9 SeC <email address hidden>:
>>>> web.form.Checkbox('checkbox1', value = 'test').render()
> '<input type="checkbox" id="checkbox1" name="checkbox1"/>'
>
> Still no 'value' attribute.

works for me. Are you sure you have the latest code?

anand@bodhi webpy (master)$ python
Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import web
>>> web.form.Checkbox('checkbox1', value = 'test').render()
'<input type="checkbox" id="checkbox1_test" value="test" name="checkbox1"/>'
>>> ^D
anand@bodhi webpy (master)$ git push
Everything up-to-date

Changed in webpy:
milestone: none → 0.33
Changed in webpy:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.