From 3ce9829b615336b0f3ac39b080c27fc8cf5af483 Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Sat, 30 Nov 2013 02:38:13 +0700 Subject: Fixed #17413 -- Serialization of form errors along with all metadata. --- tests/forms_tests/tests/test_forms.py | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'tests/forms_tests') diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 9fec9ddcaa..675565b026 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import copy import datetime +import json import warnings from django.core.files.uploadedfile import SimpleUploadedFile @@ -2031,3 +2032,40 @@ class FormsTestCase(TestCase): form = SomeForm() self.assertHTMLEqual(form.as_p(), '

') + + def test_error_dict(self): + class MyForm(Form): + foo = CharField() + bar = CharField() + + def clean(self): + raise ValidationError('Non-field error.', code='secret', params={'a': 1, 'b': 2}) + + form = MyForm({}) + self.assertEqual(form.is_valid(), False) + + errors = form.errors.as_text() + control = [ + '* foo\n * This field is required.', + '* bar\n * This field is required.', + '* __all__\n * Non-field error.', + ] + for error in control: + self.assertIn(error, errors) + + errors = form.errors.as_ul() + control = [ + '
  • foo
  • ', + '
  • bar
  • ', + '
  • __all__
  • ', + ] + for error in control: + self.assertInHTML(error, errors) + + errors = json.loads(form.errors.as_json()) + control = { + 'foo': [{'code': 'required', 'message': 'This field is required.'}], + 'bar': [{'code': 'required', 'message': 'This field is required.'}], + '__all__': [{'code': 'secret', 'message': 'Non-field error.'}] + } + self.assertEqual(errors, control) -- cgit v1.3