Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10968 - Form.errors should use Form.error_class.

 
Thanks for report and initial patch, matehat.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@11498 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7c53c2618d7dd9f04f309d195835e6abfc7e0e52 1 parent 4decf03
Luke Plant authored
4  django/forms/forms.py
@@ -243,13 +243,13 @@ def full_clean(self):
243 243
                     value = getattr(self, 'clean_%s' % name)()
244 244
                     self.cleaned_data[name] = value
245 245
             except ValidationError, e:
246  
-                self._errors[name] = e.messages
  246
+                self._errors[name] = self.error_class(e.messages)
247 247
                 if name in self.cleaned_data:
248 248
                     del self.cleaned_data[name]
249 249
         try:
250 250
             self.cleaned_data = self.clean()
251 251
         except ValidationError, e:
252  
-            self._errors[NON_FIELD_ERRORS] = e.messages
  252
+            self._errors[NON_FIELD_ERRORS] = self.error_class(e.messages)
253 253
         if self._errors:
254 254
             delattr(self, 'cleaned_data')
255 255
 
38  tests/regressiontests/forms/error_messages.py
@@ -358,4 +358,42 @@
358 358
 Traceback (most recent call last):
359 359
 ...
360 360
 ValidationError: [u'4 IS INVALID CHOICE']
  361
+
  362
+# Subclassing ErrorList #######################################################
  363
+
  364
+>>> from django.utils.safestring import mark_safe
  365
+>>>
  366
+>>> class TestForm(Form):
  367
+...      first_name = CharField()
  368
+...      last_name = CharField()
  369
+...      birthday = DateField()
  370
+...
  371
+...      def clean(self):
  372
+...          raise ValidationError("I like to be awkward.")
  373
+...
  374
+>>> class CustomErrorList(util.ErrorList):
  375
+...      def __unicode__(self):
  376
+...          return self.as_divs()
  377
+...      def as_divs(self):
  378
+...          if not self: return u''
  379
+...          return mark_safe(u'<div class="error">%s</div>'
  380
+...                    % ''.join([u'<p>%s</p>' % e for e in self]))
  381
+...
  382
+
  383
+This form should print errors the default way.
  384
+
  385
+>>> form1 = TestForm({'first_name': 'John'})
  386
+>>> print form1['last_name'].errors
  387
+<ul class="errorlist"><li>This field is required.</li></ul>
  388
+>>> print form1.errors['__all__']
  389
+<ul class="errorlist"><li>I like to be awkward.</li></ul>
  390
+
  391
+This one should wrap error groups in the customized way.
  392
+
  393
+>>> form2 = TestForm({'first_name': 'John'}, error_class=CustomErrorList)
  394
+>>> print form2['last_name'].errors
  395
+<div class="error"><p>This field is required.</p></div>
  396
+>>> print form2.errors['__all__']
  397
+<div class="error"><p>I like to be awkward.</p></div>
  398
+
361 399
 """

0 notes on commit 7c53c26

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