Django

Code

root/django/trunk/tests/regressiontests/forms/regressions.py

Revision 9067, 4.9 kB (checked in by mtredinnick, 3 weeks ago)

Fixed #9125 -- When displaying errors for a form with only hidden fields, make sure the resulting XHTML is correct.

  • Property svn:eol-style set to native
Line 
1 # -*- coding: utf-8 -*-
2 # Tests to prevent against recurrences of earlier bugs.
3
4 tests = r"""
5 It should be possible to re-use attribute dictionaries (#3810)
6 >>> from django.forms import *
7 >>> extra_attrs = {'class': 'special'}
8 >>> class TestForm(Form):
9 ...     f1 = CharField(max_length=10, widget=TextInput(attrs=extra_attrs))
10 ...     f2 = CharField(widget=TextInput(attrs=extra_attrs))
11 >>> TestForm(auto_id=False).as_p()
12 u'<p>F1: <input type="text" class="special" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" class="special" name="f2" /></p>'
13
14 #######################
15 # Tests for form i18n #
16 #######################
17 There were some problems with form translations in #3600
18
19 >>> from django.utils.translation import ugettext_lazy, activate, deactivate
20 >>> class SomeForm(Form):
21 ...     username = CharField(max_length=10, label=ugettext_lazy('Username'))
22 >>> f = SomeForm()
23 >>> print f.as_p()
24 <p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>
25
26 Translations are done at rendering time, so multi-lingual apps can define forms
27 early and still send back the right translation.
28
29 >>> activate('de')
30 >>> print f.as_p()
31 <p><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>
32 >>> activate('pl')
33 >>> f.as_p()
34 u'<p><label for="id_username">Nazwa u\u017cytkownika:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>'
35 >>> deactivate()
36
37 There was some problems with form translations in #5216
38 >>> class SomeForm(Form):
39 ...     field_1 = CharField(max_length=10, label=ugettext_lazy('field_1'))
40 ...     field_2 = CharField(max_length=10, label=ugettext_lazy('field_2'), widget=TextInput(attrs={'id': 'field_2_id'}))
41 >>> f = SomeForm()
42 >>> print f['field_1'].label_tag()
43 <label for="id_field_1">field_1</label>
44 >>> print f['field_2'].label_tag()
45 <label for="field_2_id">field_2</label>
46
47 Unicode decoding problems...
48 >>> GENDERS = ((u'\xc5', u'En tied\xe4'), (u'\xf8', u'Mies'), (u'\xdf', u'Nainen'))
49 >>> class SomeForm(Form):
50 ...     somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label=u'\xc5\xf8\xdf')
51 >>> f = SomeForm()
52 >>> f.as_p()
53 u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
54
55 Testing choice validation with UTF-8 bytestrings as input (these are the
56 Russian abbreviations "мес." and "шт.".
57
58 >>> UNITS = (('\xd0\xbc\xd0\xb5\xd1\x81.', '\xd0\xbc\xd0\xb5\xd1\x81.'), ('\xd1\x88\xd1\x82.', '\xd1\x88\xd1\x82.'))
59 >>> f = ChoiceField(choices=UNITS)
60 >>> f.clean(u'\u0448\u0442.')
61 u'\u0448\u0442.'
62 >>> f.clean('\xd1\x88\xd1\x82.')
63 u'\u0448\u0442.'
64
65 Translated error messages used to be buggy.
66 >>> activate('ru')
67 >>> f = SomeForm({})
68 >>> f.as_p()
69 u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
70 >>> deactivate()
71
72 Deep copying translated text shouldn't raise an error
73 >>> from django.utils.translation import gettext_lazy
74 >>> class CopyForm(Form):
75 ...     degree = IntegerField(widget=Select(choices=((1, gettext_lazy('test')),)))
76 >>> f = CopyForm()
77
78 #######################
79 # Miscellaneous Tests #
80 #######################
81
82 There once was a problem with Form fields called "data". Let's make sure that
83 doesn't come back.
84 >>> class DataForm(Form):
85 ...     data = CharField(max_length=10)
86 >>> f = DataForm({'data': 'xyzzy'})
87 >>> f.is_valid()
88 True
89 >>> f.cleaned_data
90 {'data': u'xyzzy'}
91
92 A form with *only* hidden fields that has errors is going to be very unusual.
93 But we can try to make sure it doesn't generate invalid XHTML. In this case,
94 the as_p() method is the tricky one, since error lists cannot be nested
95 (validly) inside p elements.
96
97 >>> class HiddenForm(Form):
98 ...     data = IntegerField(widget=HiddenInput)
99 >>> f = HiddenForm({})
100 >>> f.as_p()
101 u'<ul class="errorlist"><li>(Hidden field data) This field is required.</li></ul>\n<p> <input type="hidden" name="data" id="id_data" /></p>'
102 >>> f.as_table()
103 u'<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field data) This field is required.</li></ul><input type="hidden" name="data" id="id_data" /></td></tr>'
104
105 """
Note: See TracBrowser for help on using the browser.