Skip to content

Commit 300f26d

Browse files
Fixed #3101 -- newforms: Form.as_table() no longer puts hidden fields between <tr>s. Thanks for reporting, Eric
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4175 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 88f1dd3 commit 300f26d

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

django/newforms/forms.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,28 @@ def is_valid(self):
7575
def as_table(self):
7676
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
7777
top_errors = self.non_field_errors()
78-
output = []
78+
output, hidden_fields = [], []
7979
for name, field in self.fields.items():
8080
bf = BoundField(self, field, name)
8181
bf_errors = bf.errors # Cache in local variable.
8282
if bf.is_hidden:
8383
if bf_errors:
8484
top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
85-
output.append(str(bf))
85+
hidden_fields.append(str(bf))
8686
else:
8787
if bf_errors:
8888
output.append(u'<tr><td colspan="2">%s</td></tr>' % bf_errors)
8989
output.append(u'<tr><td>%s</td><td>%s</td></tr>' % (bf.label_tag(escape(bf.verbose_name+':')), bf))
9090
if top_errors:
9191
output.insert(0, u'<tr><td colspan="2">%s</td></tr>' % top_errors)
92+
if hidden_fields: # Insert any hidden fields in the last <td>.
93+
str_hidden = u''.join(hidden_fields)
94+
if output:
95+
last_td = output[-1]
96+
# Chop off the trailing '</td></tr>' and insert the hidden fields.
97+
output[-1] = last_td[:-10] + str_hidden + '</td></tr>'
98+
else: # If there aren't any '<td>'s in the output, just append the hidden fields.
99+
output.append(str_hidden)
92100
return u'\n'.join(output)
93101

94102
def as_ul(self):

tests/regressiontests/forms/tests.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,8 @@
17561756
17571757
HiddenInput widgets are displayed differently in the as_table() and as_ul()
17581758
output of a Form -- their verbose names are not displayed, and a separate
1759-
<tr>/<li> is not displayed.
1759+
<tr>/<li> is not displayed. They're displayed in the last <td> of the form,
1760+
directly after that <td>'s form element.
17601761
>>> class Person(Form):
17611762
... first_name = CharField()
17621763
... last_name = CharField()
@@ -1766,8 +1767,7 @@
17661767
>>> print p
17671768
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
17681769
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
1769-
<input type="hidden" name="hidden_text" />
1770-
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
1770+
<tr><td>Birthday:</td><td><input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></td></tr>
17711771
>>> print p.as_ul()
17721772
<li>First name: <input type="text" name="first_name" /></li>
17731773
<li>Last name: <input type="text" name="last_name" /></li>
@@ -1779,8 +1779,7 @@
17791779
>>> print p
17801780
<tr><td><label for="id_first_name">First name:</label></td><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
17811781
<tr><td><label for="id_last_name">Last name:</label></td><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
1782-
<input type="hidden" name="hidden_text" id="id_hidden_text" />
1783-
<tr><td><label for="id_birthday">Birthday:</label></td><td><input type="text" name="birthday" id="id_birthday" /></td></tr>
1782+
<tr><td><label for="id_birthday">Birthday:</label></td><td><input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></td></tr>
17841783
>>> print p.as_ul()
17851784
<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
17861785
<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
@@ -1796,15 +1795,25 @@
17961795
<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
17971796
<tr><td>First name:</td><td><input type="text" name="first_name" value="John" /></td></tr>
17981797
<tr><td>Last name:</td><td><input type="text" name="last_name" value="Lennon" /></td></tr>
1799-
<input type="hidden" name="hidden_text" />
1800-
<tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr>
1798+
<tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></td></tr>
18011799
>>> print p.as_ul()
18021800
<li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
18031801
<li>First name: <input type="text" name="first_name" value="John" /></li>
18041802
<li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
18051803
<input type="hidden" name="hidden_text" />
18061804
<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li>
18071805
1806+
A corner case: It's possible for a form to have only HiddenInputs.
1807+
>>> class TestForm(Form):
1808+
... foo = CharField(widget=HiddenInput)
1809+
... bar = CharField(widget=HiddenInput)
1810+
>>> p = TestForm()
1811+
>>> print p.as_table()
1812+
<input type="hidden" name="foo" /><input type="hidden" name="bar" />
1813+
>>> print p.as_ul()
1814+
<input type="hidden" name="foo" />
1815+
<input type="hidden" name="bar" />
1816+
18081817
A Form's fields are displayed in the same order in which they were defined.
18091818
>>> class TestForm(Form):
18101819
... field1 = CharField()

0 commit comments

Comments
 (0)