diff --git a/django/forms/formsets.py b/django/forms/formsets.py index 58af0ac23b8..7acf238795b 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -271,7 +271,7 @@ class BaseFormSet(StrAndUnicode): try: self.clean() except ValidationError, e: - self._non_form_errors = e.messages + self._non_form_errors = self.error_class(e.messages) def clean(self): """ diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 199b41afcb9..911549fa066 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -11,6 +11,7 @@ from django.contrib.admin.models import LogEntry, DELETION from django.contrib.admin.sites import LOGIN_FORM_KEY from django.contrib.admin.util import quote from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME +from django.forms.util import ErrorList from django.utils import formats from django.utils.cache import get_max_age from django.utils.html import escape @@ -1028,6 +1029,22 @@ class AdminViewListEditable(TestCase): response = self.client.post('/test_admin/admin/admin_views/person/', data) self.assertContains(response, "Grace is not a Zombie") + def test_non_form_errors_is_errorlist(self): + # test if non-form errors are correctly handled; ticket #12878 + data = { + "form-TOTAL_FORMS": "1", + "form-INITIAL_FORMS": "1", + "form-MAX_NUM_FORMS": "0", + + "form-0-id": "2", + "form-0-alive": "1", + "form-0-gender": "2", + } + response = self.client.post('/test_admin/admin/admin_views/person/', data) + non_form_errors = response.context['cl'].formset.non_form_errors() + self.assert_(isinstance(non_form_errors, ErrorList)) + self.assertEqual(str(non_form_errors), str(ErrorList(["Grace is not a Zombie"]))) + def test_list_editable_ordering(self): collector = Collector.objects.create(id=1, name="Frederick Clegg") diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py index 700ab7c358b..d9d4082f715 100644 --- a/tests/regressiontests/forms/formsets.py +++ b/tests/regressiontests/forms/formsets.py @@ -621,4 +621,20 @@ Make sure the management form has the correct prefix. >>> formset.management_form.prefix 'form' +# Regression test for #12878 ################################################# + +>>> data = { +... 'drinks-TOTAL_FORMS': '2', # the number of forms rendered +... 'drinks-INITIAL_FORMS': '0', # the number of forms with initial data +... 'drinks-MAX_NUM_FORMS': '0', # max number of forms +... 'drinks-0-name': 'Gin and Tonic', +... 'drinks-1-name': 'Gin and Tonic', +... } + +>>> formset = FavoriteDrinksFormSet(data, prefix='drinks') +>>> formset.is_valid() +False +>>> print formset.non_form_errors() +