mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
newforms: Added Widget.value_from_datadict hook, which allows a Widget to define how to convert its post data dictionary to a value. Implemented it for CheckboxSelectMultiple and updated unit tests
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4136 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -1471,6 +1471,7 @@ For a form with a <select>, use ChoiceField:
|
||||
<option value="J">Java</option>
|
||||
</select>
|
||||
|
||||
Add widget=RadioSelect to use that widget with a ChoiceField.
|
||||
>>> class FrameworkForm(Form):
|
||||
... name = CharField()
|
||||
... language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=RadioSelect)
|
||||
@@ -1545,6 +1546,46 @@ MultipleChoiceField is a special case, as its data is required to be a list:
|
||||
<option value="P" selected="selected">Paul McCartney</option>
|
||||
</select>
|
||||
|
||||
MultipleChoiceField can also be used with the CheckboxSelectMultiple widget.
|
||||
>>> class SongForm(Form):
|
||||
... name = CharField()
|
||||
... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=CheckboxSelectMultiple)
|
||||
>>> f = SongForm()
|
||||
>>> print f['composers']
|
||||
<ul>
|
||||
<li><label><input type="checkbox" name="composersJ" /> John Lennon</label></li>
|
||||
<li><label><input type="checkbox" name="composersP" /> Paul McCartney</label></li>
|
||||
</ul>
|
||||
>>> f = SongForm({'composers': ['J']})
|
||||
>>> print f['composers']
|
||||
<ul>
|
||||
<li><label><input checked="checked" type="checkbox" name="composersJ" /> John Lennon</label></li>
|
||||
<li><label><input type="checkbox" name="composersP" /> Paul McCartney</label></li>
|
||||
</ul>
|
||||
>>> f = SongForm({'composers': ['J', 'P']})
|
||||
>>> print f['composers']
|
||||
<ul>
|
||||
<li><label><input checked="checked" type="checkbox" name="composersJ" /> John Lennon</label></li>
|
||||
<li><label><input checked="checked" type="checkbox" name="composersP" /> Paul McCartney</label></li>
|
||||
</ul>
|
||||
|
||||
When using CheckboxSelectMultiple, the framework automatically converts the
|
||||
data in clean_data to a list of values, rather than the underlying HTML form
|
||||
field name.
|
||||
>>> f = SongForm({'name': 'Yesterday'})
|
||||
>>> f.errors
|
||||
{'composers': [u'This field is required.']}
|
||||
>>> f = SongForm({'name': 'Yesterday', 'composersJ': 'on'})
|
||||
>>> f.errors
|
||||
{}
|
||||
>>> f.clean_data
|
||||
{'composers': [u'J'], 'name': u'Yesterday'}
|
||||
>>> f = SongForm({'name': 'Yesterday', 'composersJ': 'on', 'composersP': 'on'})
|
||||
>>> f.errors
|
||||
{}
|
||||
>>> f.clean_data
|
||||
{'composers': [u'J', u'P'], 'name': u'Yesterday'}
|
||||
|
||||
There are a couple of ways to do multiple-field validation. If you want the
|
||||
validation message to be associated with a particular field, implement the
|
||||
clean_XXX() method on the Form, where XXX is the field name. As in
|
||||
|
||||
Reference in New Issue
Block a user