Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9125 -- When displaying errors for a form with only hidden fie…

…lds, make sure the resulting XHTML is correct.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9067 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9931c9e1321e3290b065bc0f5ec8353b1bcb71f3 1 parent eeb230a
Malcolm Tredinnick authored September 18, 2008
7  django/forms/forms.py
@@ -171,6 +171,13 @@ def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_
171 171
                 last_row = output[-1]
172 172
                 # Chop off the trailing row_ender (e.g. '</td></tr>') and
173 173
                 # insert the hidden fields.
  174
+                if not last_row.endswith(row_ender):
  175
+                    # This can happen in the as_p() case (and possibly others
  176
+                    # that users write): if there are only top errors, we may
  177
+                    # not be able to conscript the last row for our purposes,
  178
+                    # so insert a new, empty row.
  179
+                    last_row = normal_row % {'errors': '', 'label': '', 'field': '', 'help_text': ''}
  180
+                    output.append(last_row)
174 181
                 output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
175 182
             else:
176 183
                 # If there aren't any rows in the output, just append the
14  tests/regressiontests/forms/regressions.py
@@ -88,4 +88,18 @@
88 88
 True
89 89
 >>> f.cleaned_data
90 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
+
91 105
 """

0 notes on commit 9931c9e

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