Ticket #3512: html_class2.diff

File html_class2.diff, 2.7 KB (added by Waylan Limberg <waylan@…>, 17 years ago)

Simple, not over-ridable, but always clean

  • forms.py

     
    119119                    top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
    120120                hidden_fields.append(unicode(bf))
    121121            else:
     122                if bf_errors:
     123                    class_list = 'error '
     124                else:
     125                    class_list = ''
     126                if bf.field.required:
     127                    class_list += 'required'
     128                if classes:
     129                    html_class = ' class="%s"'% class_list.strip()
     130                else:
     131                    html_class = ''
    122132                if errors_on_separate_row and bf_errors:
    123133                    output.append(error_row % bf_errors)
    124134                label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
     
    126136                    help_text = help_text_html % field.help_text
    127137                else:
    128138                    help_text = u''
    129                 output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text})
     139                output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text, 'html_class': html_class})
    130140        if top_errors:
    131141            output.insert(0, error_row % top_errors)
    132142        if hidden_fields: # Insert any hidden fields in the last row.
     
    141151
    142152    def as_table(self):
    143153        "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)
     154        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)
    145155
    146156    def as_ul(self):
    147157        "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)
     158        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)
    149159
    150160    def as_p(self):
    151161        "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)
     162        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)
    153163
    154164    def non_field_errors(self):
    155165        """
Back to Top