diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2008-07-19 07:53:02 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2008-07-19 07:53:02 +0000 |
| commit | 649463dd348abd6d0cab890e2372e88fc452128e (patch) | |
| tree | f554f6163a30f9d0dfdb996a8322ac73d565a999 /tests/regressiontests/forms | |
| parent | b5b0febc4cd5ad51aeb1ef7b37aaca6a7632519d (diff) | |
Fixed #4412 -- Added support for optgroups, both in the model when defining choices, and in the form field and widgets when the optgroups are displayed. Thanks to Matt McClanahan <cardinal@dodds.net>, Tai Lee <real.human@mrmachine.net> and SmileyChris for their contributions at various stages in the life of this ticket.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7977 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/forms')
| -rw-r--r-- | tests/regressiontests/forms/fields.py | 57 | ||||
| -rw-r--r-- | tests/regressiontests/forms/widgets.py | 67 |
2 files changed, 116 insertions, 8 deletions
diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index 5855e58bc9..c70ff2dff3 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -980,7 +980,7 @@ False # ChoiceField ################################################################# ->>> f = ChoiceField(choices=[('1', '1'), ('2', '2')]) +>>> f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')]) >>> f.clean('') Traceback (most recent call last): ... @@ -996,9 +996,9 @@ u'1' >>> f.clean('3') Traceback (most recent call last): ... -ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] +ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] ->>> f = ChoiceField(choices=[('1', '1'), ('2', '2')], required=False) +>>> f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) >>> f.clean('') u'' >>> f.clean(None) @@ -1010,7 +1010,7 @@ u'1' >>> f.clean('3') Traceback (most recent call last): ... -ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] +ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] >>> f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')]) >>> f.clean('J') @@ -1018,7 +1018,25 @@ u'J' >>> f.clean('John') Traceback (most recent call last): ... -ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] +ValidationError: [u'Select a valid choice. John is not one of the available choices.'] + +>>> f = ChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) +>>> f.clean(1) +u'1' +>>> f.clean('1') +u'1' +>>> f.clean(3) +u'3' +>>> f.clean('3') +u'3' +>>> f.clean(5) +u'5' +>>> f.clean('5') +u'5' +>>> f.clean('6') +Traceback (most recent call last): +... +ValidationError: [u'Select a valid choice. 6 is not one of the available choices.'] # NullBooleanField ############################################################ @@ -1036,7 +1054,7 @@ False # MultipleChoiceField ######################################################### ->>> f = MultipleChoiceField(choices=[('1', '1'), ('2', '2')]) +>>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')]) >>> f.clean('') Traceback (most recent call last): ... @@ -1072,7 +1090,7 @@ Traceback (most recent call last): ... ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] ->>> f = MultipleChoiceField(choices=[('1', '1'), ('2', '2')], required=False) +>>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) >>> f.clean('') [] >>> f.clean(None) @@ -1100,6 +1118,29 @@ Traceback (most recent call last): ... ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] +>>> f = MultipleChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) +>>> f.clean([1]) +[u'1'] +>>> f.clean(['1']) +[u'1'] +>>> f.clean([1, 5]) +[u'1', u'5'] +>>> f.clean([1, '5']) +[u'1', u'5'] +>>> f.clean(['1', 5]) +[u'1', u'5'] +>>> f.clean(['1', '5']) +[u'1', u'5'] +>>> f.clean(['6']) +Traceback (most recent call last): +... +ValidationError: [u'Select a valid choice. 6 is not one of the available choices.'] +>>> f.clean(['1','6']) +Traceback (most recent call last): +... +ValidationError: [u'Select a valid choice. 6 is not one of the available choices.'] + + # ComboField ################################################################## ComboField takes a list of fields that should be used to validate a value, @@ -1165,7 +1206,7 @@ u'' >>> f.clean('fields.py') Traceback (most recent call last): ... -ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] +ValidationError: [u'Select a valid choice. fields.py is not one of the available choices.'] >>> fix_os_paths(f.clean(path + 'fields.py')) u'.../django/forms/fields.py' >>> f = forms.FilePathField(path=path, match='^.*?\.py$') diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index abb16cbcdf..40c4d01793 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -458,6 +458,35 @@ over multiple times without getting consumed: <option value="4">4</option> </select> +Choices can be nested one level in order to create HTML optgroups: +>>> w.choices=(('outer1', 'Outer 1'), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2')))) +>>> print w.render('nestchoice', None) +<select name="nestchoice"> +<option value="outer1">Outer 1</option> +<optgroup label="Group "1""> +<option value="inner1">Inner 1</option> +<option value="inner2">Inner 2</option> +</optgroup> +</select> + +>>> print w.render('nestchoice', 'outer1') +<select name="nestchoice"> +<option value="outer1" selected="selected">Outer 1</option> +<optgroup label="Group "1""> +<option value="inner1">Inner 1</option> +<option value="inner2">Inner 2</option> +</optgroup> +</select> + +>>> print w.render('nestchoice', 'inner1') +<select name="nestchoice"> +<option value="outer1">Outer 1</option> +<optgroup label="Group "1""> +<option value="inner1" selected="selected">Inner 1</option> +<option value="inner2">Inner 2</option> +</optgroup> +</select> + # NullBooleanSelect Widget #################################################### >>> w = NullBooleanSelect() @@ -626,6 +655,44 @@ True >>> w._has_changed([1, 2], [u'1', u'3']) True +# Choices can be nested one level in order to create HTML optgroups: +>>> w.choices = (('outer1', 'Outer 1'), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2')))) +>>> print w.render('nestchoice', None) +<select multiple="multiple" name="nestchoice"> +<option value="outer1">Outer 1</option> +<optgroup label="Group "1""> +<option value="inner1">Inner 1</option> +<option value="inner2">Inner 2</option> +</optgroup> +</select> + +>>> print w.render('nestchoice', ['outer1']) +<select multiple="multiple" name="nestchoice"> +<option value="outer1" selected="selected">Outer 1</option> +<optgroup label="Group "1""> +<option value="inner1">Inner 1</option> +<option value="inner2">Inner 2</option> +</optgroup> +</select> + +>>> print w.render('nestchoice', ['inner1']) +<select multiple="multiple" name="nestchoice"> +<option value="outer1">Outer 1</option> +<optgroup label="Group "1""> +<option value="inner1" selected="selected">Inner 1</option> +<option value="inner2">Inner 2</option> +</optgroup> +</select> + +>>> print w.render('nestchoice', ['outer1', 'inner2']) +<select multiple="multiple" name="nestchoice"> +<option value="outer1" selected="selected">Outer 1</option> +<optgroup label="Group "1""> +<option value="inner1">Inner 1</option> +<option value="inner2" selected="selected">Inner 2</option> +</optgroup> +</select> + # RadioSelect Widget ########################################################## >>> w = RadioSelect() |
