diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 29e387a51b..757de01f06 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -74,38 +74,38 @@ class Form(StrAndUnicode):
def as_table(self):
"Returns this form rendered as HTML
s -- excluding the
."
+ top_errors = self.non_field_errors()
output = []
- if self.errors.get(NON_FIELD_ERRORS):
- # Errors not corresponding to a particular field are displayed at the top.
- output.append(u'
%s
' % self.non_field_errors())
for name, field in self.fields.items():
bf = BoundField(self, field, name)
+ bf_errors = bf.errors
if bf.is_hidden:
- if bf.errors:
- new_errors = ErrorList(['(Hidden field %s) %s' % (name, e) for e in bf.errors])
- output.append(u'
%s
' % new_errors)
+ if bf_errors:
+ top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
output.append(str(bf))
else:
- if bf.errors:
- output.append(u'
%s
' % bf.errors)
+ if bf_errors:
+ output.append(u'
%s
' % bf_errors)
output.append(u'
%s
%s
' % (bf.label_tag(escape(bf.verbose_name+':')), bf))
+ if top_errors:
+ output.insert(0, u'
%s
' % top_errors)
return u'\n'.join(output)
def as_ul(self):
"Returns this form rendered as HTML
s -- excluding the
."
+ top_errors = self.non_field_errors()
output = []
- if self.errors.get(NON_FIELD_ERRORS):
- # Errors not corresponding to a particular field are displayed at the top.
- output.append(u'
%s
' % self.non_field_errors())
for name, field in self.fields.items():
bf = BoundField(self, field, name)
if bf.is_hidden:
- if bf.errors:
- new_errors = ErrorList(['(Hidden field %s) %s' % (name, e) for e in bf.errors])
- output.append(u'
%s
' % new_errors)
+ new_errors = bf.errors # Cache in local variable.
+ if new_errors:
+ top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in new_errors])
output.append(str(bf))
else:
output.append(u'
' % top_errors)
return u'\n'.join(output)
def non_field_errors(self):
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index bc38154d74..572d1a1f7f 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -1789,18 +1789,19 @@ With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
If a field with a HiddenInput has errors, the as_table() and as_ul() output
will include the error message(s) with the text "(Hidden field [fieldname]) "
-prepended.
+prepended. This message is displayed at the top of the output, regardless of
+its field's order in the form.
>>> p = Person({'first_name': 'John', 'last_name': 'Lennon', 'birthday': '1940-10-9'})
>>> print p
+
(Hidden field hidden_text) This field is required.
First name:
Last name:
-
(Hidden field hidden_text) This field is required.
Birthday:
>>> print p.as_ul()
+
(Hidden field hidden_text) This field is required.
First name:
Last name:
-
(Hidden field hidden_text) This field is required.