Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #20039 -- Fixed has_changed form detection for required TypedCh…

…oiceFields

Thanks Florian Apolloner for the report and the review.
Also fixes #19643.
  • Loading branch information...
commit 9883551d50e105b324d8071232bf420935844e43 1 parent 6b48349
@claudep claudep authored
View
16 django/forms/fields.py
@@ -778,14 +778,15 @@ def validate(self, value):
def valid_value(self, value):
"Check to see if the provided value is a valid choice"
+ text_value = force_text(value)
for k, v in self.choices:
if isinstance(v, (list, tuple)):
# This is an optgroup, so look inside the group for options
for k2, v2 in v:
- if value == smart_text(k2):
+ if value == k2 or text_value == force_text(k2):
return True
else:
- if value == smart_text(k):
+ if value == k or text_value == force_text(k):
return True
return False
@@ -801,7 +802,6 @@ def to_python(self, value):
right type.
"""
value = super(TypedChoiceField, self).to_python(value)
- super(TypedChoiceField, self).validate(value)
if value == self.empty_value or value in self.empty_values:
return self.empty_value
try:
@@ -810,9 +810,6 @@ def to_python(self, value):
raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})
return value
- def validate(self, value):
- pass
-
class MultipleChoiceField(ChoiceField):
hidden_widget = MultipleHiddenInput
@@ -864,7 +861,6 @@ def to_python(self, value):
right type.
"""
value = super(TypedMultipleChoiceField, self).to_python(value)
- super(TypedMultipleChoiceField, self).validate(value)
if value == self.empty_value or value in self.empty_values:
return self.empty_value
new_value = []
@@ -876,7 +872,11 @@ def to_python(self, value):
return new_value
def validate(self, value):
- pass
+ if value != self.empty_value:
+ super(TypedMultipleChoiceField, self).validate(value)
+ elif self.required:
+ raise ValidationError(self.error_messages['required'])
+
class ComboField(Field):
"""
View
10 tests/forms_tests/tests/fields.py
@@ -911,6 +911,11 @@ def test_typedchoicefield_6(self):
f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
self.assertEqual(None, f.clean(''))
+ def test_typedchoicefield_has_changed(self):
+ # has_changed should not trigger required validation
+ f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True)
+ self.assertFalse(f._has_changed(None, ''))
+
# NullBooleanField ############################################################
def test_nullbooleanfield_1(self):
@@ -1060,6 +1065,11 @@ def test_typedmultiplechoicefield_7(self):
f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
self.assertEqual(None, f.clean([]))
+ def test_typedmultiplechoicefield_has_changed(self):
+ # has_changed should not trigger required validation
+ f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True)
+ self.assertFalse(f._has_changed(None, ''))
+
# ComboField ##################################################################
def test_combofield_1(self):
View
2  tests/forms_tests/tests/regressions.py
@@ -61,10 +61,10 @@ class SomeForm(Form):
UNITS = ((b'\xd0\xbc\xd0\xb5\xd1\x81.', b'\xd0\xbc\xd0\xb5\xd1\x81.'),
(b'\xd1\x88\xd1\x82.', b'\xd1\x88\xd1\x82.'))
f = ChoiceField(choices=UNITS)
- self.assertEqual(f.clean('\u0448\u0442.'), '\u0448\u0442.')
with warnings.catch_warnings():
# Ignore UnicodeWarning
warnings.simplefilter("ignore")
+ self.assertEqual(f.clean('\u0448\u0442.'), '\u0448\u0442.')
self.assertEqual(f.clean(b'\xd1\x88\xd1\x82.'), '\u0448\u0442.')
# Translated error messages used to be buggy.
Please sign in to comment.
Something went wrong with that request. Please try again.