Ticket #6138: error_class2_test.py

File error_class2_test.py, 1.2 KB (added by Peter Mott, 15 years ago)
Line 
1"""
2>>> from django.forms import *
3>>> from django.forms.util import ErrorList
4
5Custom error_class and form using it
6
7>>> class ErrList(list):
8... def __unicode__(self):
9... return u"<div>%s</div>" % ', '.join([unicode(msg) for msg in self])
10>>> class ValidationTest(Form):
11... first_name = CharField(required=True)
12... def clean(self):
13... raise ValidationError, "Never correct"
14
15#Instance with missing field using custom class
16>>> vt = ValidationTest(error_class=ErrList, data={})
17>>> vt.errors[NON_FIELD_ERRORS].__unicode__()
18u'<div>Never correct</div>'
19>>> vt.errors['first_name'].__unicode__()
20u'<div>This field is required.</div>'
21
22Classes extending ErrorList need to be aware that field error
23messages are translation objects:
24>>> from django.utils.translation import ugettext_lazy as _
25>>> t = _('This field is required')
26>>> type(t)
27<class 'django.utils.functional.__proxy__'>
28>>> t + "x"
29u'This field is requiredx'
30>>> ', '.join([t,t,t,t])
31Traceback (most recent call last):
32...
33TypeError: sequence item 0: expected string, __proxy__ found
34>>> ', '.join([unicode(t), unicode(t)])
35u'This field is required, This field is required'
36
37"""
38
39import doctest
40doctest.testmod()
41
42
43
44
45
46
47
Back to Top