From b3085f8ae5d78adacce6968966405247b89f5dd7 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 5 Feb 2011 06:33:16 +0000 Subject: [PATCH] Fixed #15167 -- Ensure that non-form errors are always part of an ErrorList. Thanks to Harm Geerts for the report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15424 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/models.py | 4 +-- .../model_formsets_regress/tests.py | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/django/forms/models.py b/django/forms/models.py index f2b73340f6..2a3f8bdff2 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -511,7 +511,7 @@ class BaseModelFormSet(BaseFormSet): # poke error messages into the right places and mark # the form as invalid errors.append(self.get_unique_error_message(unique_check)) - form._errors[NON_FIELD_ERRORS] = self.get_form_error() + form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()]) del form.cleaned_data break # mark the data as seen @@ -542,7 +542,7 @@ class BaseModelFormSet(BaseFormSet): # poke error messages into the right places and mark # the form as invalid errors.append(self.get_date_error_message(date_check)) - form._errors[NON_FIELD_ERRORS] = self.get_form_error() + form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()]) del form.cleaned_data break seen_data.add(data) diff --git a/tests/regressiontests/model_formsets_regress/tests.py b/tests/regressiontests/model_formsets_regress/tests.py index 308bccebf1..3354fc5b7f 100644 --- a/tests/regressiontests/model_formsets_regress/tests.py +++ b/tests/regressiontests/model_formsets_regress/tests.py @@ -1,4 +1,5 @@ from django import forms +from django.forms.util import ErrorDict, ErrorList from django.forms.models import modelform_factory, inlineformset_factory, modelformset_factory from django.test import TestCase @@ -200,6 +201,31 @@ class InlineFormsetTests(TestCase): ["", ""] ) +class FormsetTests(TestCase): + def test_error_class(self): + ''' + Test the type of Formset and Form error attributes + ''' + Formset = modelformset_factory(User) + data = { + 'form-TOTAL_FORMS': u'2', + 'form-INITIAL_FORMS': u'0', + 'form-MAX_NUM_FORMS': u'0', + 'form-0-id': '', + 'form-0-username': u'apollo13', + 'form-0-serial': u'1', + 'form-1-id': '', + 'form-1-username': u'apollo13', + 'form-1-serial': u'2', + } + formset = Formset(data) + # check if the returned error classes are correct + # note: formset.errors returns a list as documented + self.assertTrue(isinstance(formset.errors, list)) + self.assertTrue(isinstance(formset.non_form_errors(), ErrorList)) + for form in formset.forms: + self.assertTrue(isinstance(form.errors, ErrorDict)) + self.assertTrue(isinstance(form.non_field_errors(), ErrorList)) class CustomWidget(forms.CharField): pass