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'%s' % self.non_field_errors()) for name, field in self.fields.items(): bf = BoundField(self, field, name) - if bf.errors: - output.append(u'%s' % bf.errors) - output.append(u'%s%s' % (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'%s' % new_errors) + output.append(str(bf)) + else: + if bf.errors: + output.append(u'%s' % bf.errors) + output.append(u'%s%s' % (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'
  • %s
  • ' % self.non_field_errors()) for name, field in self.fields.items(): bf = BoundField(self, field, name) - line = u'
  • ' - if bf.errors: - line += str(bf.errors) - line += u'%s %s
  • ' % (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'
  • %s
  • ' % new_errors) + output.append(str(bf)) + else: + output.append(u'
  • %s%s %s
  • ' % (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 = '' % (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 . + 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__(). Last name: Birthday: +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 +/
  • is not displayed. +>>> class Person(Form): +... first_name = CharField() +... last_name = CharField() +... hidden_text = CharField(widget=HiddenInput) +... birthday = DateField() +>>> p = Person() +>>> print p +First name: +Last name: + +Birthday: +>>> print p.as_ul() +
  • First name:
  • +
  • Last name:
  • + +
  • Birthday:
  • + +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 + + + + +>>> print p.as_ul() +
  • +
  • + +
  • + +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 +First name: +Last name: + + +Birthday: +>>> print p.as_ul() +
  • First name:
  • +
  • Last name:
  • +
  • + +
  • Birthday:
  • + A Form's fields are displayed in the same order in which they were defined. >>> class TestForm(Form): ... field1 = CharField()