1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

newforms: Added check_test argument to CheckboxInput and changed its render() behavior slightly. Also changed CheckboxSelectMultiple to use checkboxes with the same name, rather than checkboxes with separate names

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4148 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty
2006-11-30 22:06:08 +00:00
parent 89d1270e51
commit 468993d13e
2 changed files with 104 additions and 66 deletions

View File

@@ -91,9 +91,22 @@ class Textarea(Widget):
return u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), escape(value))
class CheckboxInput(Widget):
def __init__(self, attrs=None, check_test=bool):
# check_test is a callable that takes a value and returns True
# if the checkbox should be checked for that value.
self.attrs = attrs or {}
self.check_test = check_test
def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
if value: final_attrs['checked'] = 'checked'
try:
result = self.check_test(value)
except: # Silently catch exceptions
result = False
if result:
final_attrs['checked'] = 'checked'
if value not in ('', True, False, None):
final_attrs['value'] = smart_unicode(value) # Only add the 'value' attribute if a value is non-empty.
return u'<input%s />' % flatatt(final_attrs)
class Select(Widget):
@@ -193,19 +206,14 @@ class CheckboxSelectMultiple(SelectMultiple):
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)
cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
for option_value, option_label in chain(self.choices, choices):
option_value = smart_unicode(option_value)
field_name = name + option_value
rendered_cb = cb.render(field_name, (option_value in str_values))
rendered_cb = cb.render(name, option_value)
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 value_from_datadict(self, data, name):
data_list = [k for k, v in self.choices if data.get(name + k)]
return data_list or None
def id_for_label(self, id_):
# See the comment for RadioSelect.id_for_label()
if id_: