Commits

Maciej Wiatrzyk  committed d54607b

add CheckboxSelect widget to be used as replacement of SelectWidget(multiple=True)

  • Participants
  • Parent commits 6f30256
  • Branches widgets

Comments (0)

Files changed (1)

File wtforms/widgets/core.py

 __all__ = (
     'CheckboxInput', 'FileInput', 'HiddenInput', 'ListWidget', 'PasswordInput',
     'RadioInput', 'Select', 'SubmitInput', 'TableWidget', 'TextArea',
-    'TextInput', 'Option'
+    'TextInput', 'Option', 'CheckboxSelect'
 )
 
 
         return HTMLString(u'<option %s>%s</option>' % (html_params(**options), escape(unicode(label))))
 
 
+class CheckboxSelect(object):
+    def __call__(self, field, **kwargs):
+        html = []
+        for val, label, selected in field.iter_choices():
+            options = {
+                'id': "%s-%s" % (field.id, val),
+                'type': 'checkbox',
+                'name': field.name,
+                'value': val}
+            if selected:
+                options['checked'] = 'checked'
+            html.append(u'<input %s/><label %s>%s</label>' % (
+                html_params(**options), 
+                html_params(for_=options['id']),
+                escape(unicode(label))))
+        return HTMLString(u''.join(html))
+
+
 class Option(object):
     """
     Renders the individual option from a select field.