web.input() doesn't support array of checkboxes

301 views
Skip to first unread message

andrei

unread,
Dec 24, 2009, 4:03:37 AM12/24/09
to web.py

I would like to use an array of checkboxes, but currently webpy
doesn't support it.

I am trying to submit this form with val2 and val3 checked.

<form name="main" method="post">
<input id="chk_1" value="val1" name="chk[]" type="checkbox" />
<input id="chk_2" value="val2" name="chk[]" type="checkbox" />
<input id="chk_3" value="val3" name="chk[]" type="checkbox" />
<input id="chk_4" value="val4" name="chk[]" type="checkbox" />
<input id="chk_5" value="val5" name="chk[]" type="checkbox" />
<input type="submit"/>
</form>

If i pprint rawinput() result, i will get the desired:

<Storage {'chk[]': ['val2', 'val3']}>

But web.input() returns web.storify() of it:

<Storage {'chk[]': u'val3'}>

Maybe we should fix it somehow?
And the next step would be creating CheckboxList in Form.

andrei

unread,
Dec 24, 2009, 4:10:24 AM12/24/09
to web.py
I'm sorry!

Found the solution — passing chk=[] to web.input.

pprint(web.input(chk=[]))
<Storage {'chk': [u'val1', u'val3']}>

andrei

unread,
Dec 24, 2009, 4:44:57 AM12/24/09
to web.py
I implemented CheckboxList form element http://pastie.org/755516.
Please review it. I'd love something like this to be included in
webpy!

Anand Chitipothu

unread,
Dec 24, 2009, 4:57:01 AM12/24/09
to we...@googlegroups.com
On Thu, Dec 24, 2009 at 3:14 PM, andrei <andr...@gmail.com> wrote:
> I implemented CheckboxList form element http://pastie.org/755516.
> Please review it. I'd love something like this to be included in
> webpy!

It looks good except for the following:

* There is no way to add class/id to individual checkboxes.
* form should be able to figure out that it should expect multiple
values and shouldn't have to depend on user giving the input.
form = myform() should work even with CheckboxList. I have been
postponing its implementation just because of this reason.

andrei

unread,
Dec 24, 2009, 5:26:49 AM12/24/09
to web.py

Additional attributes for individual checkboxes can be passed in args
argument (could be the easiest implementation), or maybe it would be
better to turn args argument into list of form.Checkbox elements and
ignore their name attributes.

As for the second one, I'm not quite sure what you mean. Does it mean
that it requires passing additional "chk=[]" to web.input()?

On Dec 24, 12:57 pm, Anand Chitipothu <anandol...@gmail.com> wrote:
> On Thu, Dec 24, 2009 at 3:14 PM, andrei <andre...@gmail.com> wrote:
> > I implemented CheckboxList form elementhttp://pastie.org/755516.

Anand Chitipothu

unread,
Dec 24, 2009, 5:32:52 AM12/24/09
to we...@googlegroups.com
On Thu, Dec 24, 2009 at 3:56 PM, andrei <andr...@gmail.com> wrote:
>
> Additional attributes for individual checkboxes can be passed in args
> argument (could be the easiest implementation), or maybe it would be
> better to turn args argument into list of form.Checkbox elements and
> ignore their name attributes.

See, it is getting complicated.

> As for the second one, I'm not quite sure what you mean. Does it mean
> that it requires passing additional "chk=[]" to web.input()?

Yes.

form = myform() shouldn't raise exception.

andrei

unread,
Dec 24, 2009, 5:48:25 AM12/24/09
to web.py
Ok, got your point, thanks!


On Dec 24, 1:32 pm, Anand Chitipothu <anandol...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages