Fixed #12878. Formset-wide errors are now rendered properly as html.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12548 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Joseph Kocherhans 2010-02-23 20:55:42 +00:00
parent c8fa110093
commit ea3853e8d2
3 changed files with 34 additions and 1 deletions

View File

@ -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):
"""

View File

@ -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")

View File

@ -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()
<ul class="errorlist"><li>You may only specify a drink once.</li></ul>
"""