newforms: Changed Table.as_table() and Table.as_ul() to put hidden-form errors at the top of the output rather than in field order

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-12-07 05:35:39 +00:00
parent 558e07a9bc
commit 12e9a84429
2 changed files with 18 additions and 17 deletions

View File

@ -74,38 +74,38 @@ class Form(StrAndUnicode):
def as_table(self): def as_table(self):
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>." "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
top_errors = self.non_field_errors()
output = [] output = []
if self.errors.get(NON_FIELD_ERRORS):
# Errors not corresponding to a particular field are displayed at the top.
output.append(u'<tr><td colspan="2">%s</td></tr>' % self.non_field_errors())
for name, field in self.fields.items(): for name, field in self.fields.items():
bf = BoundField(self, field, name) bf = BoundField(self, field, name)
bf_errors = bf.errors
if bf.is_hidden: if bf.is_hidden:
if bf.errors: if bf_errors:
new_errors = ErrorList(['(Hidden field %s) %s' % (name, e) for e in bf.errors]) top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
output.append(u'<tr><td colspan="2">%s</td></tr>' % new_errors)
output.append(str(bf)) output.append(str(bf))
else: else:
if bf.errors: if bf_errors:
output.append(u'<tr><td colspan="2">%s</td></tr>' % bf.errors) output.append(u'<tr><td colspan="2">%s</td></tr>' % bf_errors)
output.append(u'<tr><td>%s</td><td>%s</td></tr>' % (bf.label_tag(escape(bf.verbose_name+':')), bf)) output.append(u'<tr><td>%s</td><td>%s</td></tr>' % (bf.label_tag(escape(bf.verbose_name+':')), bf))
if top_errors:
output.insert(0, u'<tr><td colspan="2">%s</td></tr>' % top_errors)
return u'\n'.join(output) return u'\n'.join(output)
def as_ul(self): def as_ul(self):
"Returns this form rendered as HTML <li>s -- excluding the <ul></ul>." "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
top_errors = self.non_field_errors()
output = [] output = []
if self.errors.get(NON_FIELD_ERRORS):
# Errors not corresponding to a particular field are displayed at the top.
output.append(u'<li>%s</li>' % self.non_field_errors())
for name, field in self.fields.items(): for name, field in self.fields.items():
bf = BoundField(self, field, name) bf = BoundField(self, field, name)
if bf.is_hidden: if bf.is_hidden:
if bf.errors: new_errors = bf.errors # Cache in local variable.
new_errors = ErrorList(['(Hidden field %s) %s' % (name, e) for e in bf.errors]) if new_errors:
output.append(u'<li>%s</li>' % new_errors) top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in new_errors])
output.append(str(bf)) output.append(str(bf))
else: else:
output.append(u'<li>%s%s %s</li>' % (bf.errors, bf.label_tag(escape(bf.verbose_name+':')), bf)) output.append(u'<li>%s%s %s</li>' % (bf.errors, bf.label_tag(escape(bf.verbose_name+':')), bf))
if top_errors:
output.insert(0, u'<li>%s</li>' % top_errors)
return u'\n'.join(output) return u'\n'.join(output)
def non_field_errors(self): def non_field_errors(self):

View File

@ -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 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]) " 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'}) >>> p = Person({'first_name': 'John', 'last_name': 'Lennon', 'birthday': '1940-10-9'})
>>> print p >>> print p
<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
<tr><td>First name:</td><td><input type="text" name="first_name" value="John" /></td></tr> <tr><td>First name:</td><td><input type="text" name="first_name" value="John" /></td></tr>
<tr><td>Last name:</td><td><input type="text" name="last_name" value="Lennon" /></td></tr> <tr><td>Last name:</td><td><input type="text" name="last_name" value="Lennon" /></td></tr>
<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
<input type="hidden" name="hidden_text" /> <input type="hidden" name="hidden_text" />
<tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr> <tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr>
>>> print p.as_ul() >>> print p.as_ul()
<li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
<li>First name: <input type="text" name="first_name" value="John" /></li> <li>First name: <input type="text" name="first_name" value="John" /></li>
<li>Last name: <input type="text" name="last_name" value="Lennon" /></li> <li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
<li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
<input type="hidden" name="hidden_text" /> <input type="hidden" name="hidden_text" />
<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li> <li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li>