From 9222091de8fa2fcb4fedd74ac99b65c88d295135 Mon Sep 17 00:00:00 2001 From: Honza Král Date: Fri, 11 Sep 2009 21:23:55 +0000 Subject: [soc2009/model-validation] Merged to trunk at r11499 git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11513 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/forms/error_messages.py | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'tests/regressiontests/forms') diff --git a/tests/regressiontests/forms/error_messages.py b/tests/regressiontests/forms/error_messages.py index 3f6cfcac80..12ec8a1585 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.

+ """ -- cgit v1.3