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)) return u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), escape(value))
class CheckboxInput(Widget): 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): def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, type='checkbox', name=name) 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) return u'<input%s />' % flatatt(final_attrs)
class Select(Widget): class Select(Widget):
@ -193,19 +206,14 @@ class CheckboxSelectMultiple(SelectMultiple):
final_attrs = self.build_attrs(attrs, name=name) final_attrs = self.build_attrs(attrs, name=name)
output = [u'<ul>'] output = [u'<ul>']
str_values = set([smart_unicode(v) for v in value]) # Normalize to strings. 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): for option_value, option_label in chain(self.choices, choices):
option_value = smart_unicode(option_value) option_value = smart_unicode(option_value)
field_name = name + option_value rendered_cb = cb.render(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'<li><label>%s %s</label></li>' % (rendered_cb, escape(smart_unicode(option_label))))
output.append(u'</ul>') output.append(u'</ul>')
return u'\n'.join(output) 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_): def id_for_label(self, id_):
# See the comment for RadioSelect.id_for_label() # See the comment for RadioSelect.id_for_label()
if id_: if id_:

View File

@ -164,10 +164,18 @@ u'<textarea class="fun" name="msg">\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0
>>> w = CheckboxInput() >>> w = CheckboxInput()
>>> w.render('is_cool', '') >>> w.render('is_cool', '')
u'<input type="checkbox" name="is_cool" />' u'<input type="checkbox" name="is_cool" />'
>>> w.render('is_cool', None)
u'<input type="checkbox" name="is_cool" />'
>>> w.render('is_cool', False) >>> w.render('is_cool', False)
u'<input type="checkbox" name="is_cool" />' u'<input type="checkbox" name="is_cool" />'
>>> w.render('is_cool', True) >>> w.render('is_cool', True)
u'<input checked="checked" type="checkbox" name="is_cool" />' u'<input checked="checked" type="checkbox" name="is_cool" />'
Using any value that's not in ('', None, False, True) will check the checkbox
and set the 'value' attribute.
>>> w.render('is_cool', 'foo')
u'<input checked="checked" type="checkbox" name="is_cool" value="foo" />'
>>> w.render('is_cool', False, attrs={'class': 'pretty'}) >>> w.render('is_cool', False, attrs={'class': 'pretty'})
u'<input type="checkbox" name="is_cool" class="pretty" />' u'<input type="checkbox" name="is_cool" class="pretty" />'
@ -181,6 +189,29 @@ u'<input type="checkbox" class="pretty" name="is_cool" />'
>>> w.render('is_cool', '', attrs={'class': 'special'}) >>> w.render('is_cool', '', attrs={'class': 'special'})
u'<input type="checkbox" class="special" name="is_cool" />' u'<input type="checkbox" class="special" name="is_cool" />'
You can pass 'check_test' to the constructor. This is a callable that takes the
value and returns True if the box should be checked.
>>> w = CheckboxInput(check_test=lambda value: value.startswith('hello'))
>>> w.render('greeting', '')
u'<input type="checkbox" name="greeting" />'
>>> w.render('greeting', 'hello')
u'<input checked="checked" type="checkbox" name="greeting" value="hello" />'
>>> w.render('greeting', 'hello there')
u'<input checked="checked" type="checkbox" name="greeting" value="hello there" />'
>>> w.render('greeting', 'hello & goodbye')
u'<input checked="checked" type="checkbox" name="greeting" value="hello &amp; goodbye" />'
A subtlety: If the 'check_test' argument cannot handle a value and raises any
exception during its __call__, then the exception will be swallowed and the box
will not be checked. In this example, the 'check_test' assumes the value has a
startswith() method, which fails for the values True, False and None.
>>> w.render('greeting', True)
u'<input type="checkbox" name="greeting" />'
>>> w.render('greeting', False)
u'<input type="checkbox" name="greeting" />'
>>> w.render('greeting', None)
u'<input type="checkbox" name="greeting" />'
# Select Widget ############################################################### # Select Widget ###############################################################
>>> w = Select() >>> w = Select()
@ -488,71 +519,71 @@ beatle J R Ringo False
>>> w = CheckboxSelectMultiple() >>> w = CheckboxSelectMultiple()
>>> print w.render('beatles', ['J'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) >>> print w.render('beatles', ['J'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
<ul> <ul>
<li><label><input checked="checked" type="checkbox" name="beatlesJ" /> John</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input type="checkbox" name="beatlesP" /> Paul</label></li> <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
<li><label><input type="checkbox" name="beatlesG" /> George</label></li> <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
<li><label><input type="checkbox" name="beatlesR" /> Ringo</label></li> <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
</ul> </ul>
>>> print w.render('beatles', ['J', 'P'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) >>> print w.render('beatles', ['J', 'P'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
<ul> <ul>
<li><label><input checked="checked" type="checkbox" name="beatlesJ" /> John</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input checked="checked" type="checkbox" name="beatlesP" /> Paul</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="P" /> Paul</label></li>
<li><label><input type="checkbox" name="beatlesG" /> George</label></li> <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
<li><label><input type="checkbox" name="beatlesR" /> Ringo</label></li> <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
</ul> </ul>
>>> print w.render('beatles', ['J', 'P', 'R'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) >>> print w.render('beatles', ['J', 'P', 'R'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
<ul> <ul>
<li><label><input checked="checked" type="checkbox" name="beatlesJ" /> John</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input checked="checked" type="checkbox" name="beatlesP" /> Paul</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="P" /> Paul</label></li>
<li><label><input type="checkbox" name="beatlesG" /> George</label></li> <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
<li><label><input checked="checked" type="checkbox" name="beatlesR" /> Ringo</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="R" /> Ringo</label></li>
</ul> </ul>
If the value is None, none of the options are selected: If the value is None, none of the options are selected:
>>> print w.render('beatles', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) >>> print w.render('beatles', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
<ul> <ul>
<li><label><input type="checkbox" name="beatlesJ" /> John</label></li> <li><label><input type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input type="checkbox" name="beatlesP" /> Paul</label></li> <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
<li><label><input type="checkbox" name="beatlesG" /> George</label></li> <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
<li><label><input type="checkbox" name="beatlesR" /> Ringo</label></li> <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
</ul> </ul>
If the value corresponds to a label (but not to an option value), none of the options are selected: If the value corresponds to a label (but not to an option value), none of the options are selected:
>>> print w.render('beatles', ['John'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) >>> print w.render('beatles', ['John'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
<ul> <ul>
<li><label><input type="checkbox" name="beatlesJ" /> John</label></li> <li><label><input type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input type="checkbox" name="beatlesP" /> Paul</label></li> <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
<li><label><input type="checkbox" name="beatlesG" /> George</label></li> <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
<li><label><input type="checkbox" name="beatlesR" /> Ringo</label></li> <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
</ul> </ul>
If multiple values are given, but some of them are not valid, the valid ones are selected: If multiple values are given, but some of them are not valid, the valid ones are selected:
>>> print w.render('beatles', ['J', 'G', 'foo'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) >>> print w.render('beatles', ['J', 'G', 'foo'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
<ul> <ul>
<li><label><input checked="checked" type="checkbox" name="beatlesJ" /> John</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input type="checkbox" name="beatlesP" /> Paul</label></li> <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
<li><label><input checked="checked" type="checkbox" name="beatlesG" /> George</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="G" /> George</label></li>
<li><label><input type="checkbox" name="beatlesR" /> Ringo</label></li> <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
</ul> </ul>
The value is compared to its str(): The value is compared to its str():
>>> print w.render('nums', [2], choices=[('1', '1'), ('2', '2'), ('3', '3')]) >>> print w.render('nums', [2], choices=[('1', '1'), ('2', '2'), ('3', '3')])
<ul> <ul>
<li><label><input type="checkbox" name="nums1" /> 1</label></li> <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
<li><label><input checked="checked" type="checkbox" name="nums2" /> 2</label></li> <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
<li><label><input type="checkbox" name="nums3" /> 3</label></li> <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
</ul> </ul>
>>> print w.render('nums', ['2'], choices=[(1, 1), (2, 2), (3, 3)]) >>> print w.render('nums', ['2'], choices=[(1, 1), (2, 2), (3, 3)])
<ul> <ul>
<li><label><input type="checkbox" name="nums1" /> 1</label></li> <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
<li><label><input checked="checked" type="checkbox" name="nums2" /> 2</label></li> <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
<li><label><input type="checkbox" name="nums3" /> 3</label></li> <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
</ul> </ul>
>>> print w.render('nums', [2], choices=[(1, 1), (2, 2), (3, 3)]) >>> print w.render('nums', [2], choices=[(1, 1), (2, 2), (3, 3)])
<ul> <ul>
<li><label><input type="checkbox" name="nums1" /> 1</label></li> <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
<li><label><input checked="checked" type="checkbox" name="nums2" /> 2</label></li> <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
<li><label><input type="checkbox" name="nums3" /> 3</label></li> <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
</ul> </ul>
The 'choices' argument can be any iterable: The 'choices' argument can be any iterable:
@ -561,34 +592,34 @@ The 'choices' argument can be any iterable:
... yield (i, i) ... yield (i, i)
>>> print w.render('nums', [2], choices=get_choices()) >>> print w.render('nums', [2], choices=get_choices())
<ul> <ul>
<li><label><input type="checkbox" name="nums0" /> 0</label></li> <li><label><input type="checkbox" name="nums" value="0" /> 0</label></li>
<li><label><input type="checkbox" name="nums1" /> 1</label></li> <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
<li><label><input checked="checked" type="checkbox" name="nums2" /> 2</label></li> <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
<li><label><input type="checkbox" name="nums3" /> 3</label></li> <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
<li><label><input type="checkbox" name="nums4" /> 4</label></li> <li><label><input type="checkbox" name="nums" value="4" /> 4</label></li>
</ul> </ul>
You can also pass 'choices' to the constructor: You can also pass 'choices' to the constructor:
>>> w = CheckboxSelectMultiple(choices=[(1, 1), (2, 2), (3, 3)]) >>> w = CheckboxSelectMultiple(choices=[(1, 1), (2, 2), (3, 3)])
>>> print w.render('nums', [2]) >>> print w.render('nums', [2])
<ul> <ul>
<li><label><input type="checkbox" name="nums1" /> 1</label></li> <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
<li><label><input checked="checked" type="checkbox" name="nums2" /> 2</label></li> <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
<li><label><input type="checkbox" name="nums3" /> 3</label></li> <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
</ul> </ul>
If 'choices' is passed to both the constructor and render(), then they'll both be in the output: If 'choices' is passed to both the constructor and render(), then they'll both be in the output:
>>> print w.render('nums', [2], choices=[(4, 4), (5, 5)]) >>> print w.render('nums', [2], choices=[(4, 4), (5, 5)])
<ul> <ul>
<li><label><input type="checkbox" name="nums1" /> 1</label></li> <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
<li><label><input checked="checked" type="checkbox" name="nums2" /> 2</label></li> <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
<li><label><input type="checkbox" name="nums3" /> 3</label></li> <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
<li><label><input type="checkbox" name="nums4" /> 4</label></li> <li><label><input type="checkbox" name="nums" value="4" /> 4</label></li>
<li><label><input type="checkbox" name="nums5" /> 5</label></li> <li><label><input type="checkbox" name="nums" value="5" /> 5</label></li>
</ul> </ul>
>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
u'<ul>\n<li><label><input type="checkbox" name="nums1" /> 1</label></li>\n<li><label><input type="checkbox" name="nums2" /> 2</label></li>\n<li><label><input type="checkbox" name="nums3" /> 3</label></li>\n<li><label><input checked="checked" type="checkbox" name="nums\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="checkbox" name="nums\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>' u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>\n<li><label><input type="checkbox" name="nums" value="2" /> 2</label></li>\n<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>\n<li><label><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>'
########## ##########
# Fields # # Fields #
@ -1599,34 +1630,33 @@ MultipleChoiceField can also be used with the CheckboxSelectMultiple widget.
>>> f = SongForm() >>> f = SongForm()
>>> print f['composers'] >>> print f['composers']
<ul> <ul>
<li><label><input type="checkbox" name="composersJ" /> John Lennon</label></li> <li><label><input type="checkbox" name="composers" value="J" /> John Lennon</label></li>
<li><label><input type="checkbox" name="composersP" /> Paul McCartney</label></li> <li><label><input type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
</ul> </ul>
>>> f = SongForm({'composers': ['J']}) >>> f = SongForm({'composers': ['J']})
>>> print f['composers'] >>> print f['composers']
<ul> <ul>
<li><label><input checked="checked" type="checkbox" name="composersJ" /> John Lennon</label></li> <li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li>
<li><label><input type="checkbox" name="composersP" /> Paul McCartney</label></li> <li><label><input type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
</ul> </ul>
>>> f = SongForm({'composers': ['J', 'P']}) >>> f = SongForm({'composers': ['J', 'P']})
>>> print f['composers'] >>> print f['composers']
<ul> <ul>
<li><label><input checked="checked" type="checkbox" name="composersJ" /> John Lennon</label></li> <li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li>
<li><label><input checked="checked" type="checkbox" name="composersP" /> Paul McCartney</label></li> <li><label><input checked="checked" type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
</ul> </ul>
When using CheckboxSelectMultiple, the framework automatically converts the When using CheckboxSelectMultiple, the framework expects a list of input and
data in clean_data to a list of values, rather than the underlying HTML form returns a list of input.
field name.
>>> f = SongForm({'name': 'Yesterday'}) >>> f = SongForm({'name': 'Yesterday'})
>>> f.errors >>> f.errors
{'composers': [u'This field is required.']} {'composers': [u'This field is required.']}
>>> f = SongForm({'name': 'Yesterday', 'composersJ': 'on'}) >>> f = SongForm({'name': 'Yesterday', 'composers': ['J']})
>>> f.errors >>> f.errors
{} {}
>>> f.clean_data >>> f.clean_data
{'composers': [u'J'], 'name': u'Yesterday'} {'composers': [u'J'], 'name': u'Yesterday'}
>>> f = SongForm({'name': 'Yesterday', 'composersJ': 'on', 'composersP': 'on'}) >>> f = SongForm({'name': 'Yesterday', 'composers': ['J', 'P']})
>>> f.errors >>> f.errors
{} {}
>>> f.clean_data >>> f.clean_data