From e130031fd2e1fa6946e56c73efab0c3ac67ebaea Mon Sep 17 00:00:00 2001 From: Adrian Holovaty <adrian@holovaty.com> Date: Thu, 30 Nov 2006 17:07:40 +0000 Subject: [PATCH] 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 --- django/newforms/forms.py | 29 +++++++++++----- django/newforms/widgets.py | 3 ++ tests/regressiontests/forms/tests.py | 50 ++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 393f576dca..4bc6173249 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -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 diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 51bbaf0896..a7764b8dc5 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -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' diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index bfa9d07237..2fb468ace2 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1689,6 +1689,56 @@ subclass' __init__(). <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()