Ticket #3512: html_class3.2.diff

File html_class3.2.diff, 3.3 KB (added by Chris Beaven, 17 years ago)

slightly improved, should use same logic for html_class2.diff if that is preferred

  • django/newforms/forms.py

     
    107107        """
    108108        return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name
    109109
    110     def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
     110    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row, html_class_list=[]):
    111111        "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
    112112        top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
    113113        output, hidden_fields = [], []
     
    119119                    top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
    120120                hidden_fields.append(unicode(bf))
    121121            else:
     122                class_list = html_class_list # Reset for each loop.
     123                if bf_errors:
     124                    class_list.append('error')
     125                if bf.field.required:
     126                    class_list.append('required')
     127                if class_list:
     128                    html_class = ' class="%s"' % ' '.join(class_list)
     129                else:
     130                    html_class = ''
    122131                if errors_on_separate_row and bf_errors:
    123132                    output.append(error_row % bf_errors)
    124133                label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
     
    126135                    help_text = help_text_html % field.help_text
    127136                else:
    128137                    help_text = u''
    129                 output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text})
     138                output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text, 'html_class': html_class})
    130139        if top_errors:
    131140            output.insert(0, error_row % top_errors)
    132141        if hidden_fields: # Insert any hidden fields in the last row.
     
    141150
    142151    def as_table(self):
    143152        "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
    144         return self._html_output(u'<tr><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
     153        return self._html_output(u'<tr%(html_class)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
    145154
    146155    def as_ul(self):
    147156        "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
    148         return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)
     157        return self._html_output(u'<li%(html_class)s>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)
    149158
    150159    def as_p(self):
    151160        "Returns this form rendered as HTML <p>s."
    152         return self._html_output(u'<p>%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True)
     161        return self._html_output(u'<p%(html_class)s>%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True)
    153162
    154163    def non_field_errors(self):
    155164        """
Back to Top