Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15167 -- Ensure that non-form errors are always part of an Err…

…orList. 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
  • Loading branch information...
commit b3085f8ae5d78adacce6968966405247b89f5dd7 1 parent 7f65c33
Russell Keith-Magee authored February 05, 2011
4  django/forms/models.py
@@ -511,7 +511,7 @@ def validate_unique(self):
511 511
                         # poke error messages into the right places and mark
512 512
                         # the form as invalid
513 513
                         errors.append(self.get_unique_error_message(unique_check))
514  
-                        form._errors[NON_FIELD_ERRORS] = self.get_form_error()
  514
+                        form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()])
515 515
                         del form.cleaned_data
516 516
                         break
517 517
                     # mark the data as seen
@@ -542,7 +542,7 @@ def validate_unique(self):
542 542
                         # poke error messages into the right places and mark
543 543
                         # the form as invalid
544 544
                         errors.append(self.get_date_error_message(date_check))
545  
-                        form._errors[NON_FIELD_ERRORS] = self.get_form_error()
  545
+                        form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()])
546 546
                         del form.cleaned_data
547 547
                         break
548 548
                     seen_data.add(data)
26  tests/regressiontests/model_formsets_regress/tests.py
... ...
@@ -1,4 +1,5 @@
1 1
 from django import forms
  2
+from django.forms.util import ErrorDict, ErrorList
2 3
 from django.forms.models import modelform_factory, inlineformset_factory, modelformset_factory
3 4
 from django.test import TestCase
4 5
 
@@ -200,6 +201,31 @@ def test_save_as_new_with_new_inlines(self):
200 201
             ["<Host: matrix.de.eu.dal.net>", "<Host: tranquility.hub.dal.net>"]
201 202
             )
202 203
 
  204
+class FormsetTests(TestCase):
  205
+    def test_error_class(self):
  206
+        '''
  207
+        Test the type of Formset and Form error attributes
  208
+        '''
  209
+        Formset = modelformset_factory(User)
  210
+        data = {
  211
+            'form-TOTAL_FORMS': u'2',
  212
+            'form-INITIAL_FORMS': u'0',
  213
+            'form-MAX_NUM_FORMS': u'0',
  214
+            'form-0-id': '',
  215
+            'form-0-username': u'apollo13',
  216
+            'form-0-serial': u'1',
  217
+            'form-1-id': '',
  218
+            'form-1-username': u'apollo13',
  219
+            'form-1-serial': u'2',
  220
+        }
  221
+        formset = Formset(data)
  222
+        # check if the returned error classes are correct
  223
+        # note: formset.errors returns a list as documented
  224
+        self.assertTrue(isinstance(formset.errors, list))
  225
+        self.assertTrue(isinstance(formset.non_form_errors(), ErrorList))
  226
+        for form in formset.forms:
  227
+            self.assertTrue(isinstance(form.errors, ErrorDict))
  228
+            self.assertTrue(isinstance(form.non_field_errors(), ErrorList))
203 229
 
204 230
 class CustomWidget(forms.CharField):
205 231
     pass

0 notes on commit b3085f8

Please sign in to comment.
Something went wrong with that request. Please try again.