set dom id on the first input of radio choices tls-sprint
authorsylvain.thenault@logilab.fr
Wed, 15 Apr 2009 17:24:34 +0200
branchtls-sprint
changeset 1367 01bb5b727fe3
parent 1366 fd5bd1a63c3f
child 1368 c0138f349ff4
set dom id on the first input of radio choices
web/formwidgets.py
--- a/web/formwidgets.py	Wed Apr 15 17:24:12 2009 +0200
+++ b/web/formwidgets.py	Wed Apr 15 17:24:34 2009 +0200
@@ -203,19 +203,19 @@
     input will be generated for each possible value.
     """ 
     type = 'radio'
-    setdomid = False # by default, don't set id attribute on radio input
     
     def render(self, form, field):
         name, curvalues, attrs = self._render_attrs(form, field)
+        domid = attrs.pop('id', None)
         options = []
-        for label, value in field.vocabulary(form):
+        for i, (label, value) in enumerate(field.vocabulary(form)):
+            iattrs = attrs.copy()
+            if i == 0 and domid is not None:
+                iattrs['id'] = domid
             if value in curvalues:
-                tag = tags.input(name=name, type=self.type, value=value,
-                                 checked='checked', **attrs)
-            else:
-                tag = tags.input(name=name, type=self.type, value=value, **attrs)
-            tag += label + '<br/>'
-            options.append(tag)
+                iattrs['checked'] = u'checked'
+            tag = tags.input(name=name, type=self.type, value=value, **iattrs)
+            options.append(tag + label + '<br/>')
         return '\n'.join(options)