[webpy] Radio buttons with different value and description

288 views
Skip to first unread message

FHSM

unread,
May 10, 2010, 12:21:20 PM5/10/10
to web.py
I'm trying to make a form that has radio buttons with different values
and descriptive text. For example, this:
<input type="radio" id="sex" value="xy" name="sex"/> male
<input type="radio" id="sex" value="xx" name="sex"/> female
instead of:
<input type="radio" id="sex" value="male" name="sex"/> male
<input type="radio" id="sex" value="female" name="sex"/> female

I'm trying to do so using web.form and the following code:
simple_form = form.Form(
form.Radio('sex',
[('xy', 'male'),('xx', 'female')],
description='Select your sex',
),
)

This code results in the following unexpected markup:
<input type="radio" id="sex" value="(&#39;xy&#39;,
&#39;male&#39;)" name="sex"/> male
<input type="radio" id="sex" value="(&#39;xx&#39;,
&#39;female&#39;)" name="sex"/> female

It looks like the description text is working as expected but instead
of using the first element of the tuple the whole tuple is being used
for the value attribute.

What am I doing incorrectly? Is it possible this is a bug? The
following patch (http://pastebin.com/DJjHgds3) seems to make form.py
behave as I expected, but I know too little about the library to know
what other ramifications this may have:
--- form.py 2010-03-20 13:40:07.000000000 -0400
+++ form.py 2010-05-10 11:50:03.000000000 -0400
@@ -255,14 +255,15 @@
def render(self):
x = '<span>'
for arg in self.args:
+ attrs = self.attrs.copy()
if isinstance(arg, (tuple, list)):
value, desc= arg
+ attrs['value'] = arg[0]
else:
value, desc = arg, arg
- attrs = self.attrs.copy()
+ attrs['value'] = arg
attrs['name'] = self.name
attrs['type'] = 'radio'
- attrs['value'] = arg
if self.value == arg:
attrs['checked'] = 'checked'
x += '<input %s/> %s' % (attrs, net.websafe(desc))

Thanks for the help.

--
You received this message because you are subscribed to the Google Groups "web.py" group.
To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.

FHSM

unread,
May 10, 2010, 8:08:33 PM5/10/10
to web.py
The patch I posted broke validation and would not work with radio
buttons that had numbers as values this is the patch I should have
attached (http://pastebin.com/VgBwpVWU):
--- form.py 2010-03-20 13:40:07.000000000 -0400
+++ form_update.py 2010-05-10 13:05:30.000000000 -0400
@@ -262,8 +262,8 @@
attrs = self.attrs.copy()
attrs['name'] = self.name
attrs['type'] = 'radio'
- attrs['value'] = arg
- if self.value == arg:
+ attrs['value'] = value
+ if self.value == str(value):
attrs['checked'] = 'checked'
x += '<input %s/> %s' % (attrs, net.websafe(desc))
x += '</span>'

Question still stands: bug or user error?
Thanks.
Finn

FHSM

unread,
May 12, 2010, 3:39:49 PM5/12/10
to web.py
I took the silence as a sign and posted an issue on github. Hopefully
the bug is in the code and note between my ears. Here's the link:
http://github.com/webpy/webpy/issues#issue/13

Additionally, I separated out two separate issues with a second one
covering forms with radio buttons or dropdowns that have the value
attribute set to an integer. Because the posted form does not contain
ints the test of equality in the form render fails. As a result the
state of these elements is lost when redisplaying a form after an
invalid submission attempt.
I think this patch would fix the problem:
243c243
< if self.value == value: select_p = '
selected="selected"'
---
> if self.value == str(value): select_p = ' selected="selected"'
266c266
< if self.value == arg:
---
> if self.value == str(arg):

This issue is here: http://github.com/webpy/webpy/issues#issue/14

Have I missed the correct use of the framework / implications of these
changes or do they seem reasonable?
Reply all
Reply to author
Forward
0 new messages