diff --git a/django/forms/utils.py b/django/forms/utils.py index 3582384293..df75e94f81 100644 --- a/django/forms/utils.py +++ b/django/forms/utils.py @@ -79,7 +79,7 @@ class ErrorDict(dict): @python_2_unicode_compatible -class ErrorList(UserList): +class ErrorList(UserList, list): """ A collection of errors that knows how to display itself in various formats. """ diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 675565b026..f2a77cb089 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -15,6 +15,7 @@ from django.forms import ( NullBooleanField, PasswordInput, RadioSelect, Select, SplitDateTimeField, Textarea, TextInput, ValidationError, widgets, ) +from django.forms.utils import ErrorList from django.http import QueryDict from django.template import Template, Context from django.test import TestCase @@ -2069,3 +2070,27 @@ class FormsTestCase(TestCase): '__all__': [{'code': 'secret', 'message': 'Non-field error.'}] } self.assertEqual(errors, control) + + def test_error_list(self): + e = ErrorList() + e.append('Foo') + e.append(ValidationError('Foo%(bar)s', code='foobar', params={'bar': 'bar'})) + + self.assertTrue(isinstance(e, list)) + self.assertIn('Foo', e) + self.assertIn('Foo', forms.ValidationError(e)) + + self.assertEqual( + e.as_text(), + '* Foo\n* Foobar' + ) + + self.assertEqual( + e.as_ul(), + '' + ) + + self.assertEqual( + json.loads(e.as_json()), + [{"message": "Foo", "code": ""}, {"message": "Foobar", "code": "foobar"}] + )