diff options
| author | Honza Král <honza.kral@gmail.com> | 2009-09-11 21:23:55 +0000 |
|---|---|---|
| committer | Honza Král <honza.kral@gmail.com> | 2009-09-11 21:23:55 +0000 |
| commit | 9222091de8fa2fcb4fedd74ac99b65c88d295135 (patch) | |
| tree | 32069d6f3f1fce7bc06d257113999f41a0ce31c1 /tests/regressiontests/forms | |
| parent | 78d75b86e3e45ec3ca9f2ace21255922046dd509 (diff) | |
[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
Diffstat (limited to 'tests/regressiontests/forms')
| -rw-r--r-- | tests/regressiontests/forms/error_messages.py | 38 |
1 files changed, 38 insertions, 0 deletions
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'<div class="error">%s</div>' +... % ''.join([u'<p>%s</p>' % e for e in self])) +... + +This form should print errors the default way. + +>>> form1 = TestForm({'first_name': 'John'}) +>>> print form1['last_name'].errors +<ul class="errorlist"><li>This field is required.</li></ul> +>>> print form1.errors['__all__'] +<ul class="errorlist"><li>I like to be awkward.</li></ul> + +This one should wrap error groups in the customized way. + +>>> form2 = TestForm({'first_name': 'John'}, error_class=CustomErrorList) +>>> print form2['last_name'].errors +<div class="error"><p>This field is required.</p></div> +>>> print form2.errors['__all__'] +<div class="error"><p>I like to be awkward.</p></div> + """ |
