1 | """
|
---|
2 | >>> from django.forms import *
|
---|
3 | >>> from django.forms.util import ErrorList
|
---|
4 |
|
---|
5 | Custom 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__()
|
---|
18 | u'<div>Never correct</div>'
|
---|
19 | >>> vt.errors['first_name'].__unicode__()
|
---|
20 | u'<div>This field is required.</div>'
|
---|
21 |
|
---|
22 | Classes extending ErrorList need to be aware that field error
|
---|
23 | messages 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"
|
---|
29 | u'This field is requiredx'
|
---|
30 | >>> ', '.join([t,t,t,t])
|
---|
31 | Traceback (most recent call last):
|
---|
32 | ...
|
---|
33 | TypeError: sequence item 0: expected string, __proxy__ found
|
---|
34 | >>> ', '.join([unicode(t), unicode(t)])
|
---|
35 | u'This field is required, This field is required'
|
---|
36 |
|
---|
37 | """
|
---|
38 |
|
---|
39 | import doctest
|
---|
40 | doctest.testmod()
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|