Ticket #4860: checkboxselectmultiple.diff

File checkboxselectmultiple.diff, 15.7 KB (added by sandro, 17 years ago)
Line 
1Index: django/newforms/widgets.py
2===================================================================
3--- django/newforms/widgets.py (revision 6862)
4+++ django/newforms/widgets.py (working copy)
5@@ -367,8 +367,9 @@
6 cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
7 option_value = force_unicode(option_value)
8 rendered_cb = cb.render(name, option_value)
9- output.append(u'<li><label>%s %s</label></li>' % (rendered_cb,
10- conditional_escape(force_unicode(option_label))))
11+ for_id = final_attrs.get('id', option_value)
12+ output.append(u'<li>%s <label for="%s">%s</label></li>' % (rendered_cb, for_id, conditional_escape(force_unicode(option_label))))
13 output.append(u'</ul>')
14 return mark_safe(u'\n'.join(output))
15
16Index: tests/regressiontests/forms/forms.py
17===================================================================
18--- tests/regressiontests/forms/forms.py (revision 6862)
19+++ tests/regressiontests/forms/forms.py (working copy)
20@@ -494,20 +494,20 @@
21 >>> f = SongForm(auto_id=False)
22 >>> print f['composers']
23 <ul>
24-<li><label><input type="checkbox" name="composers" value="J" /> John Lennon</label></li>
25-<li><label><input type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
26+<li><input type="checkbox" name="composers" value="J" /> <label for="J">John Lennon</label></li>
27+<li><input type="checkbox" name="composers" value="P" /> <label for="P">Paul McCartney</label></li>
28 </ul>
29 >>> f = SongForm({'composers': ['J']}, auto_id=False)
30 >>> print f['composers']
31 <ul>
32-<li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li>
33-<li><label><input type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
34+<li><input checked="checked" type="checkbox" name="composers" value="J" /> <label for="J">John Lennon</label></li>
35+<li><input type="checkbox" name="composers" value="P" /> <label for="P">Paul McCartney</label></li>
36 </ul>
37 >>> f = SongForm({'composers': ['J', 'P']}, auto_id=False)
38 >>> print f['composers']
39 <ul>
40-<li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li>
41-<li><label><input checked="checked" type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
42+<li><input checked="checked" type="checkbox" name="composers" value="J" /> <label for="J">John Lennon</label></li>
43+<li><input checked="checked" type="checkbox" name="composers" value="P" /> <label for="P">Paul McCartney</label></li>
44 </ul>
45
46 Regarding auto_id, CheckboxSelectMultiple is a special case. Each checkbox
47@@ -516,8 +516,8 @@
48 >>> f = SongForm(auto_id='%s_id')
49 >>> print f['composers']
50 <ul>
51-<li><label><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li>
52-<li><label><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
53+<li><input type="checkbox" name="composers" value="J" id="composers_id_0" /> <label for="composers_id_0">John Lennon</label></li>
54+<li><input type="checkbox" name="composers" value="P" id="composers_id_1" /> <label for="composers_id_1">Paul McCartney</label></li>
55 </ul>
56
57 Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict
58Index: tests/regressiontests/forms/widgets.py
59===================================================================
60--- tests/regressiontests/forms/widgets.py (revision 6862)
61+++ tests/regressiontests/forms/widgets.py (working copy)
62@@ -762,71 +762,71 @@
63 >>> w = CheckboxSelectMultiple()
64 >>> print w.render('beatles', ['J'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
65 <ul>
66-<li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
67-<li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
68-<li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
69-<li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
70+<li><input checked="checked" type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
71+<li><input type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
72+<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
73+<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
74 </ul>
75 >>> print w.render('beatles', ['J', 'P'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
76 <ul>
77-<li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
78-<li><label><input checked="checked" type="checkbox" name="beatles" value="P" /> Paul</label></li>
79-<li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
80-<li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
81+<li><input checked="checked" type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
82+<li><input checked="checked" type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
83+<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
84+<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
85 </ul>
86 >>> print w.render('beatles', ['J', 'P', 'R'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
87 <ul>
88-<li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
89-<li><label><input checked="checked" type="checkbox" name="beatles" value="P" /> Paul</label></li>
90-<li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
91-<li><label><input checked="checked" type="checkbox" name="beatles" value="R" /> Ringo</label></li>
92+<li><input checked="checked" type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
93+<li><input checked="checked" type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
94+<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
95+<li><input checked="checked" type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
96 </ul>
97
98 If the value is None, none of the options are selected:
99 >>> print w.render('beatles', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
100 <ul>
101-<li><label><input type="checkbox" name="beatles" value="J" /> John</label></li>
102-<li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
103-<li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
104-<li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
105+<li><input type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
106+<li><input type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
107+<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
108+<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
109 </ul>
110
111 If the value corresponds to a label (but not to an option value), none of the options are selected:
112 >>> print w.render('beatles', ['John'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
113 <ul>
114-<li><label><input type="checkbox" name="beatles" value="J" /> John</label></li>
115-<li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
116-<li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
117-<li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
118+<li><input type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
119+<li><input type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
120+<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
121+<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
122 </ul>
123
124 If multiple values are given, but some of them are not valid, the valid ones are selected:
125 >>> print w.render('beatles', ['J', 'G', 'foo'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
126 <ul>
127-<li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
128-<li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
129-<li><label><input checked="checked" type="checkbox" name="beatles" value="G" /> George</label></li>
130-<li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
131+<li><input checked="checked" type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
132+<li><input type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
133+<li><input checked="checked" type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
134+<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
135 </ul>
136
137 The value is compared to its str():
138 >>> print w.render('nums', [2], choices=[('1', '1'), ('2', '2'), ('3', '3')])
139 <ul>
140-<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
141-<li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
142-<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
143+<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
144+<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
145+<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
146 </ul>
147 >>> print w.render('nums', ['2'], choices=[(1, 1), (2, 2), (3, 3)])
148 <ul>
149-<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
150-<li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
151-<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
152+<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
153+<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
154+<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
155 </ul>
156 >>> print w.render('nums', [2], choices=[(1, 1), (2, 2), (3, 3)])
157 <ul>
158-<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
159-<li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
160-<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
161+<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
162+<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
163+<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
164 </ul>
165
166 The 'choices' argument can be any iterable:
167@@ -835,45 +835,45 @@
168 ... yield (i, i)
169 >>> print w.render('nums', [2], choices=get_choices())
170 <ul>
171-<li><label><input type="checkbox" name="nums" value="0" /> 0</label></li>
172-<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
173-<li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
174-<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
175-<li><label><input type="checkbox" name="nums" value="4" /> 4</label></li>
176+<li><input type="checkbox" name="nums" value="0" /> <label for="0">0</label></li>
177+<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
178+<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
179+<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
180+<li><input type="checkbox" name="nums" value="4" /> <label for="4">4</label></li>
181 </ul>
182
183 You can also pass 'choices' to the constructor:
184 >>> w = CheckboxSelectMultiple(choices=[(1, 1), (2, 2), (3, 3)])
185 >>> print w.render('nums', [2])
186 <ul>
187-<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
188-<li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
189-<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
190+<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
191+<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
192+<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
193 </ul>
194
195 If 'choices' is passed to both the constructor and render(), then they'll both be in the output:
196 >>> print w.render('nums', [2], choices=[(4, 4), (5, 5)])
197 <ul>
198-<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
199-<li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
200-<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
201-<li><label><input type="checkbox" name="nums" value="4" /> 4</label></li>
202-<li><label><input type="checkbox" name="nums" value="5" /> 5</label></li>
203+<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
204+<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
205+<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
206+<li><input type="checkbox" name="nums" value="4" /> <label for="4">4</label></li>
207+<li><input type="checkbox" name="nums" value="5" /> <label for="5">5</label></li>
208 </ul>
209
210 # Choices are escaped correctly
211 >>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you &gt; me'))))
212 <ul>
213-<li><label><input type="checkbox" name="escape" value="1" /> 1</label></li>
214-<li><label><input type="checkbox" name="escape" value="2" /> 2</label></li>
215-<li><label><input type="checkbox" name="escape" value="3" /> 3</label></li>
216-<li><label><input type="checkbox" name="escape" value="bad" /> you &amp; me</label></li>
217-<li><label><input type="checkbox" name="escape" value="good" /> you &gt; me</label></li>
218+<li><input type="checkbox" name="escape" value="1" /> <label for="1">1</label></li>
219+<li><input type="checkbox" name="escape" value="2" /> <label for="2">2</label></li>
220+<li><input type="checkbox" name="escape" value="3" /> <label for="3">3</label></li>
221+<li><input type="checkbox" name="escape" value="bad" /> <label for="bad">you &amp; me</label></li>
222+<li><input type="checkbox" name="escape" value="good" /> <label for="good">you &gt; me</label></li>
223 </ul>
224
225 # Unicode choices are correctly rendered as HTML
226 >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
227-u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>\n<li><label><input type="checkbox" name="nums" value="2" /> 2</label></li>\n<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>\n<li><label><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>'
228+u'<ul>\n<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>\n<li><input type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>\n<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>\n<li><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> <label for="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> <label for="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</label></li>\n</ul>'
229
230 # MultiWidget #################################################################
231
Back to Top