From 7c53c2618d7dd9f04f309d195835e6abfc7e0e52 Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Fri, 11 Sep 2009 10:47:40 +0000 Subject: [PATCH] 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 --- django/forms/forms.py | 4 +- tests/regressiontests/forms/error_messages.py | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/django/forms/forms.py b/django/forms/forms.py index 1b9faa7e4d..0b7c2e2338 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -243,13 +243,13 @@ class BaseForm(StrAndUnicode): value = getattr(self, 'clean_%s' % name)() self.cleaned_data[name] = value except ValidationError, e: - self._errors[name] = e.messages + self._errors[name] = self.error_class(e.messages) if name in self.cleaned_data: del self.cleaned_data[name] try: self.cleaned_data = self.clean() except ValidationError, e: - self._errors[NON_FIELD_ERRORS] = e.messages + self._errors[NON_FIELD_ERRORS] = self.error_class(e.messages) if self._errors: delattr(self, 'cleaned_data') diff --git a/tests/regressiontests/forms/error_messages.py b/tests/regressiontests/forms/error_messages.py index ec91b57a06..b7224dbde0 100644 --- a/tests/regressiontests/forms/error_messages.py +++ b/tests/regressiontests/forms/error_messages.py @@ -358,4 +358,42 @@ ValidationError: [u'NOT A LIST OF VALUES'] Traceback (most recent call last): ... ValidationError: [u'4 IS INVALID CHOICE'] + +# Subclassing ErrorList ####################################################### + +>>> from django.utils.safestring import mark_safe +>>> +>>> class TestForm(Form): +... first_name = CharField() +... last_name = CharField() +... birthday = DateField() +... +... def clean(self): +... raise ValidationError("I like to be awkward.") +... +>>> class CustomErrorList(util.ErrorList): +... def __unicode__(self): +... return self.as_divs() +... def as_divs(self): +... if not self: return u'' +... return mark_safe(u'
%s
' +... % ''.join([u'

%s

' % e for e in self])) +... + +This form should print errors the default way. + +>>> form1 = TestForm({'first_name': 'John'}) +>>> print form1['last_name'].errors + +>>> print form1.errors['__all__'] + + +This one should wrap error groups in the customized way. + +>>> form2 = TestForm({'first_name': 'John'}, error_class=CustomErrorList) +>>> print form2['last_name'].errors +

This field is required.

+>>> print form2.errors['__all__'] +

I like to be awkward.

+ """