From 649463dd348abd6d0cab890e2372e88fc452128e Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 19 Jul 2008 07:53:02 +0000 Subject: 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 , Tai Lee 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 --- tests/regressiontests/forms/fields.py | 57 +++++++++++++++++++++++++---- tests/regressiontests/forms/widgets.py | 67 ++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 8 deletions(-) (limited to 'tests/regressiontests/forms') 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: +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) + + +>>> print w.render('nestchoice', 'outer1') + + +>>> print w.render('nestchoice', 'inner1') + + # 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) + + +>>> print w.render('nestchoice', ['outer1']) + + +>>> print w.render('nestchoice', ['inner1']) + + +>>> print w.render('nestchoice', ['outer1', 'inner2']) + + # RadioSelect Widget ########################################################## >>> w = RadioSelect() -- cgit v1.3