summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2006-11-29 03:02:26 +0000
committerAdrian Holovaty <adrian@holovaty.com>2006-11-29 03:02:26 +0000
commit8aae90c0e5ff45fbcb6c380f8d952e87e7506ab8 (patch)
tree92de9e9499efcbe2dd4bf517b75bfe2f4ea20553 /django
parentfe4af48ec8fd16acffa8c148b5c2308d6137f386 (diff)
newforms: Implemented CheckboxSelectMultiple
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4132 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/newforms/widgets.py25
1 files changed, 22 insertions, 3 deletions
diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py
index b4eac248fc..d0ea678df5 100644
--- a/django/newforms/widgets.py
+++ b/django/newforms/widgets.py
@@ -5,7 +5,7 @@ HTML Widget classes
__all__ = (
'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'FileInput',
'Textarea', 'CheckboxInput',
- 'Select', 'SelectMultiple', 'RadioSelect',
+ 'Select', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple',
)
from util import smart_unicode
@@ -176,5 +176,24 @@ class RadioSelect(Select):
return id_
id_for_label = classmethod(id_for_label)
-class CheckboxSelectMultiple(Widget):
- pass
+class CheckboxSelectMultiple(SelectMultiple):
+ def render(self, name, value, attrs=None, choices=()):
+ if value is None: value = []
+ final_attrs = self.build_attrs(attrs, name=name)
+ output = [u'<ul>']
+ str_values = set([smart_unicode(v) for v in value]) # Normalize to strings.
+ cb = CheckboxInput(final_attrs)
+ for option_value, option_label in chain(self.choices, choices):
+ option_value = smart_unicode(option_value)
+ field_name = unicode(name + option_value)
+ rendered_cb = cb.render(field_name, (option_value in str_values))
+ output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(smart_unicode(option_label))))
+ output.append(u'</ul>')
+ return u'\n'.join(output)
+
+ def id_for_label(self, id_):
+ # See the comment for RadioSelect.id_for_label()
+ if id_:
+ id_ += '_0'
+ return id_
+ id_for_label = classmethod(id_for_label)