Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #3082 -- newforms: Changed Form as_table() and as_ul() not to d…

…isplay 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
  • Loading branch information...
commit e130031fd2e1fa6946e56c73efab0c3ac67ebaea 1 parent 4dca65c
Adrian Holovaty adrianholovaty authored
29 django/newforms/forms.py
View
@@ -80,9 +80,15 @@ def as_table(self):
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 @@ def as_ul(self):
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 @@ def label_tag(self, contents=None):
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
3  django/newforms/widgets.py
View
@@ -23,6 +23,8 @@
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'
50 tests/regressiontests/forms/tests.py
View
@@ -1689,6 +1689,56 @@
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
+HiddenInput widgets are displayed differently in the as_table() and as_ul()
+output of a Form -- their verbose names are not displayed, and a separate
+<tr>/<li> is not displayed.
+>>> class Person(Form):
+... first_name = CharField()
+... last_name = CharField()
+... hidden_text = CharField(widget=HiddenInput)
+... birthday = DateField()
+>>> p = Person()
+>>> print p
+<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
+<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
+<input type="hidden" name="hidden_text" />
+<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
+>>> print p.as_ul()
+<li>First name: <input type="text" name="first_name" /></li>
+<li>Last name: <input type="text" name="last_name" /></li>
+<input type="hidden" name="hidden_text" />
+<li>Birthday: <input type="text" name="birthday" /></li>
+
+With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
+>>> p = Person(auto_id='id_%s')
+>>> print p
+<tr><td><label for="id_first_name">First name:</label></td><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
+<tr><td><label for="id_last_name">Last name:</label></td><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
+<input type="hidden" name="hidden_text" id="id_hidden_text" />
+<tr><td><label for="id_birthday">Birthday:</label></td><td><input type="text" name="birthday" id="id_birthday" /></td></tr>
+>>> print p.as_ul()
+<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
+<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
+<input type="hidden" name="hidden_text" id="id_hidden_text" />
+<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>
+
+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.
+>>> p = Person({'first_name': 'John', 'last_name': 'Lennon', 'birthday': '1940-10-9'})
+>>> print p
+<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 colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
+<input type="hidden" name="hidden_text" />
+<tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr>
+>>> print p.as_ul()
+<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><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
+<input type="hidden" name="hidden_text" />
+<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li>
+
A Form's fields are displayed in the same order in which they were defined.
>>> class TestForm(Form):
... field1 = CharField()
Please sign in to comment.
Something went wrong with that request. Please try again.