Ticket #4024: forms.py.diff

File forms.py.diff, 2.5 KB (added by Anders Conbere <aconbere@…>, 17 years ago)

adds the ability to attach css classes to fields in generated output

  • forms.py

     
    120120                    top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
    121121                hidden_fields.append(unicode(bf))
    122122            else:
     123                css_classes = []
    123124                if errors_on_separate_row and bf_errors:
    124125                    output.append(error_row % bf_errors)
    125126                label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
     
    127128                    help_text = help_text_html % field.help_text
    128129                else:
    129130                    help_text = u''
    130                 output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text})
     131                if field.required:
     132                    css_classes.append('required')
     133                classes = u'class="%s"' % u' '.join(css_classes)
     134                output.append(normal_row % {'classes': classes, 'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text})
    131135        if top_errors:
    132136            output.insert(0, error_row % top_errors)
    133137        if hidden_fields: # Insert any hidden fields in the last row.
     
    142146
    143147    def as_table(self):
    144148        "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
    145         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)
     149        return self._html_output(u'<tr %(classes)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)
    146150
    147151    def as_ul(self):
    148152        "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
    149         return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)
     153        return self._html_output(u'<li %(classes)s>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)
    150154
    151155    def as_p(self):
    152156        "Returns this form rendered as HTML <p>s."
    153         return self._html_output(u'<p>%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True)
     157        return self._html_output(u'<p %(classes)s>%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True)
    154158
    155159    def non_field_errors(self):
    156160        """
Back to Top