Code

Ticket #20039: 20039-1.diff

File 20039-1.diff, 4.3 KB (added by claudep, 16 months ago)
Line 
1diff --git a/django/forms/fields.py b/django/forms/fields.py
2index bc37701..8c0a061 100644
3--- a/django/forms/fields.py
4+++ b/django/forms/fields.py
5@@ -782,10 +782,10 @@ class ChoiceField(Field):
6             if isinstance(v, (list, tuple)):
7                 # This is an optgroup, so look inside the group for options
8                 for k2, v2 in v:
9-                    if value == smart_text(k2):
10+                    if value == k2 or force_text(value) == force_text(k2):
11                         return True
12             else:
13-                if value == smart_text(k):
14+                if value == k or force_text(value) == force_text(k):
15                     return True
16         return False
17 
18@@ -801,7 +801,6 @@ class TypedChoiceField(ChoiceField):
19         right type.
20         """
21         value = super(TypedChoiceField, self).to_python(value)
22-        super(TypedChoiceField, self).validate(value)
23         if value == self.empty_value or value in self.empty_values:
24             return self.empty_value
25         try:
26@@ -810,9 +809,6 @@ class TypedChoiceField(ChoiceField):
27             raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})
28         return value
29 
30-    def validate(self, value):
31-        pass
32-
33 
34 class MultipleChoiceField(ChoiceField):
35     hidden_widget = MultipleHiddenInput
36@@ -864,7 +860,6 @@ class TypedMultipleChoiceField(MultipleChoiceField):
37         right type.
38         """
39         value = super(TypedMultipleChoiceField, self).to_python(value)
40-        super(TypedMultipleChoiceField, self).validate(value)
41         if value == self.empty_value or value in self.empty_values:
42             return self.empty_value
43         new_value = []
44@@ -876,7 +871,12 @@ class TypedMultipleChoiceField(MultipleChoiceField):
45         return new_value
46 
47     def validate(self, value):
48-        pass
49+        if value == self.empty_value:
50+            if self.required:
51+                raise ValidationError(self.error_messages['required'])
52+        else:
53+            super(TypedMultipleChoiceField, self).validate(value)
54+
55 
56 class ComboField(Field):
57     """
58diff --git a/tests/forms_tests/tests/fields.py b/tests/forms_tests/tests/fields.py
59index 3d3206e..95e14c4 100644
60--- a/tests/forms_tests/tests/fields.py
61+++ b/tests/forms_tests/tests/fields.py
62@@ -911,6 +911,11 @@ class FieldsTests(SimpleTestCase):
63         f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
64         self.assertEqual(None, f.clean(''))
65 
66+    def test_typedchoicefield_has_changed(self):
67+        # has_changed should not trigger required validation
68+        f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True)
69+        self.assertFalse(f._has_changed(None, ''))
70+
71     # NullBooleanField ############################################################
72 
73     def test_nullbooleanfield_1(self):
74@@ -1060,6 +1065,11 @@ class FieldsTests(SimpleTestCase):
75         f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
76         self.assertEqual(None, f.clean([]))
77 
78+    def test_typedmultiplechoicefield_has_changed(self):
79+        # has_changed should not trigger required validation
80+        f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True)
81+        self.assertFalse(f._has_changed(None, ''))
82+
83    # ComboField ##################################################################
84 
85     def test_combofield_1(self):
86diff --git a/tests/forms_tests/tests/regressions.py b/tests/forms_tests/tests/regressions.py
87index 4249b8a..ba741bc 100644
88--- a/tests/forms_tests/tests/regressions.py
89+++ b/tests/forms_tests/tests/regressions.py
90@@ -61,10 +61,10 @@ class FormsRegressionsTestCase(TestCase):
91         UNITS = ((b'\xd0\xbc\xd0\xb5\xd1\x81.', b'\xd0\xbc\xd0\xb5\xd1\x81.'),
92                  (b'\xd1\x88\xd1\x82.', b'\xd1\x88\xd1\x82.'))
93         f = ChoiceField(choices=UNITS)
94-        self.assertEqual(f.clean('\u0448\u0442.'), '\u0448\u0442.')
95         with warnings.catch_warnings():
96             # Ignore UnicodeWarning
97             warnings.simplefilter("ignore")
98+            self.assertEqual(f.clean('\u0448\u0442.'), '\u0448\u0442.')
99             self.assertEqual(f.clean(b'\xd1\x88\xd1\x82.'), '\u0448\u0442.')
100 
101         # Translated error messages used to be buggy.