Fixed #23674 -- Fixed a crash when a MultiValueField has invalid data.

This commit is contained in:
Grzegorz Slusarek 2014-12-08 23:37:59 +01:00 committed by Tim Graham
parent 43041ee48c
commit 0dea81cd6d
3 changed files with 23 additions and 1 deletions

View File

@ -1104,7 +1104,11 @@ class MultiValueField(Field):
if not isinstance(initial, list):
initial = self.widget.decompress(initial)
for field, initial, data in zip(self.fields, initial, data):
if field.has_changed(field.to_python(initial), data):
try:
initial = field.to_python(initial)
except ValidationError:
return True
if field.has_changed(initial, data):
return True
return False

View File

@ -119,3 +119,5 @@ Bugfixes
* Fixed bug in ``makemigrations`` that created broken migration files when
dealing with multiple table inheritance and inheriting from more than one
model (:ticket:`23956`).
* Fixed a crash when a ``MultiValueField`` has invalid data (:ticket:`23674`).

View File

@ -2006,6 +2006,22 @@ class FormsTestCase(TestCase):
self.assertIsNot(field2.fields, field.fields)
self.assertIsNot(field2.fields[0].choices, field.fields[0].choices)
def test_multivalue_initial_data(self):
"""
#23674 -- invalid initial data should not break form.changed_data()
"""
class DateAgeField(MultiValueField):
def __init__(self, fields=(), *args, **kwargs):
fields = (DateField(label="Date"), IntegerField(label="Age"))
super(DateAgeField, self).__init__(fields=fields, *args, **kwargs)
class DateAgeForm(Form):
date_age = DateAgeField()
data = {"date_age": ["1998-12-06", 16]}
form = DateAgeForm(data, initial={"date_age": ["200-10-10", 14]})
self.assertTrue(form.has_changed())
def test_multivalue_optional_subfields(self):
class PhoneField(MultiValueField):
def __init__(self, *args, **kwargs):