Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 300f26defffcfa7b474823b3272c378f6a8af043 1 parent 88f1dd3
Adrian Holovaty authored
12  django/newforms/forms.py
@@ -75,20 +75,28 @@ def is_valid(self):
75 75
     def as_table(self):
76 76
         "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
77 77
         top_errors = self.non_field_errors()
78  
-        output = []
  78
+        output, hidden_fields = [], []
79 79
         for name, field in self.fields.items():
80 80
             bf = BoundField(self, field, name)
81 81
             bf_errors = bf.errors # Cache in local variable.
82 82
             if bf.is_hidden:
83 83
                 if bf_errors:
84 84
                     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))
86 86
             else:
87 87
                 if bf_errors:
88 88
                     output.append(u'<tr><td colspan="2">%s</td></tr>' % bf_errors)
89 89
                 output.append(u'<tr><td>%s</td><td>%s</td></tr>' % (bf.label_tag(escape(bf.verbose_name+':')), bf))
90 90
         if top_errors:
91 91
             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)
92 100
         return u'\n'.join(output)
93 101
 
94 102
     def as_ul(self):
23  tests/regressiontests/forms/tests.py
@@ -1756,7 +1756,8 @@
1756 1756
 
1757 1757
 HiddenInput widgets are displayed differently in the as_table() and as_ul()
1758 1758
 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.
1760 1761
 >>> class Person(Form):
1761 1762
 ...     first_name = CharField()
1762 1763
 ...     last_name = CharField()
@@ -1766,8 +1767,7 @@
1766 1767
 >>> print p
1767 1768
 <tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
1768 1769
 <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>
1771 1771
 >>> print p.as_ul()
1772 1772
 <li>First name: <input type="text" name="first_name" /></li>
1773 1773
 <li>Last name: <input type="text" name="last_name" /></li>
@@ -1779,8 +1779,7 @@
1779 1779
 >>> print p
1780 1780
 <tr><td><label for="id_first_name">First name:</label></td><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
1781 1781
 <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>
1784 1783
 >>> print p.as_ul()
1785 1784
 <li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
1786 1785
 <li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
@@ -1796,8 +1795,7 @@
1796 1795
 <tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
1797 1796
 <tr><td>First name:</td><td><input type="text" name="first_name" value="John" /></td></tr>
1798 1797
 <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>
1801 1799
 >>> print p.as_ul()
1802 1800
 <li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
1803 1801
 <li>First name: <input type="text" name="first_name" value="John" /></li>
@@ -1805,6 +1803,17 @@
1805 1803
 <input type="hidden" name="hidden_text" />
1806 1804
 <li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li>
1807 1805
 
  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
+
1808 1817
 A Form's fields are displayed in the same order in which they were defined.
1809 1818
 >>> class TestForm(Form):
1810 1819
 ...     field1 = CharField()

0 notes on commit 300f26d

Please sign in to comment.
Something went wrong with that request. Please try again.