mirror of
https://github.com/django/django.git
synced 2025-10-25 22:56:12 +00:00
Fixed #3082 -- newforms: Changed Form as_table() and as_ul() not to display verbose names for hidden fields, and to add field-name prefix to error messages to avoid user confusion. Also added unit tests.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4146 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -80,9 +80,15 @@ class Form(object):
|
||||
output.append(u'<tr><td colspan="2">%s</td></tr>' % self.non_field_errors())
|
||||
for name, field in self.fields.items():
|
||||
bf = BoundField(self, field, name)
|
||||
if 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))
|
||||
if bf.is_hidden:
|
||||
if bf.errors:
|
||||
new_errors = ErrorList(['(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))
|
||||
else:
|
||||
if 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))
|
||||
return u'\n'.join(output)
|
||||
|
||||
def as_ul(self):
|
||||
@@ -93,11 +99,13 @@ class Form(object):
|
||||
output.append(u'<li>%s</li>' % self.non_field_errors())
|
||||
for name, field in self.fields.items():
|
||||
bf = BoundField(self, field, name)
|
||||
line = u'<li>'
|
||||
if bf.errors:
|
||||
line += str(bf.errors)
|
||||
line += u'%s %s</li>' % (bf.label_tag(escape(bf.verbose_name+':')), bf)
|
||||
output.append(line)
|
||||
if bf.is_hidden:
|
||||
if bf.errors:
|
||||
new_errors = ErrorList(['(Hidden field %s) %s' % (name, e) for e in bf.errors])
|
||||
output.append(u'<li>%s</li>' % new_errors)
|
||||
output.append(str(bf))
|
||||
else:
|
||||
output.append(u'<li>%s%s %s</li>' % (bf.errors, bf.label_tag(escape(bf.verbose_name+':')), bf))
|
||||
return u'\n'.join(output)
|
||||
|
||||
def non_field_errors(self):
|
||||
@@ -222,6 +230,11 @@ class BoundField(object):
|
||||
contents = '<label for="%s">%s</label>' % (widget.id_for_label(id_), contents)
|
||||
return contents
|
||||
|
||||
def _is_hidden(self):
|
||||
"Returns True if this BoundField's widget is hidden."
|
||||
return self._field.widget.is_hidden
|
||||
is_hidden = property(_is_hidden)
|
||||
|
||||
def _auto_id(self):
|
||||
"""
|
||||
Calculates and returns the ID attribute for this BoundField, if the
|
||||
|
||||
@@ -23,6 +23,8 @@ flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs
|
||||
|
||||
class Widget(object):
|
||||
requires_data_list = False # Determines whether render()'s 'value' argument should be a list.
|
||||
is_hidden = False # Determines whether this corresponds to an <input type="hidden">.
|
||||
|
||||
def __init__(self, attrs=None):
|
||||
self.attrs = attrs or {}
|
||||
|
||||
@@ -76,6 +78,7 @@ class PasswordInput(Input):
|
||||
|
||||
class HiddenInput(Input):
|
||||
input_type = 'hidden'
|
||||
is_hidden = True
|
||||
|
||||
class FileInput(Input):
|
||||
input_type = 'file'
|
||||
|
||||
Reference in New Issue
Block a user