summaryrefslogtreecommitdiff
path: root/tests/regressiontests/forms/tests.py
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2006-11-29 17:00:34 +0000
committerAdrian Holovaty <adrian@holovaty.com>2006-11-29 17:00:34 +0000
commit4a3ad338d67c2412fb4c0f7b44b7a5651a7ec1a6 (patch)
tree7cd29fd58941e52476ef994a2eed65de36a62cae /tests/regressiontests/forms/tests.py
parenta1cd3c9f522760057ea3e9007d2b07ed340d745f (diff)
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
Diffstat (limited to 'tests/regressiontests/forms/tests.py')
-rw-r--r--tests/regressiontests/forms/tests.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index 59fbc0cdea..e3b7553155 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -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