Ticket #17922: 17922-2.diff

File 17922-2.diff, 1.9 KB (added by Claude Paroz, 12 years ago)

Set required_css_class on label

  • django/forms/forms.py

    diff --git a/django/forms/forms.py b/django/forms/forms.py
    index 3299c2b..3dc27de 100644
    a b class BaseForm(object):  
    170170                    if self.label_suffix:
    171171                        if label[-1] not in ':?.!':
    172172                            label = format_html('{0}{1}', label, self.label_suffix)
    173                     label = bf.label_tag(label) or ''
     173                    label = bf.label_tag(label, attrs={}) or ''
    174174                else:
    175175                    label = ''
    176176
    class BoundField(object):  
    507507        widget = self.field.widget
    508508        id_ = widget.attrs.get('id') or self.auto_id
    509509        if id_:
     510            if (attrs is None and self.field.required and
     511                    hasattr(self.form, 'required_css_class')):
     512                attrs = {'class': self.form.required_css_class}
    510513            attrs = attrs and flatatt(attrs) or ''
    511514            contents = format_html('<label for="{0}"{1}>{2}</label>',
    512515                                   widget.id_for_label(id_), attrs, contents
  • tests/regressiontests/forms/tests/forms.py

    diff --git a/tests/regressiontests/forms/tests/forms.py b/tests/regressiontests/forms/tests/forms.py
    index a8a28ba..56f722f 100644
    a b class FormsTestCase(TestCase):  
    17591759<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
    17601760<tr class="required error"><th><label for="id_age">Age:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="age" id="id_age" /></td></tr>""")
    17611761
     1762        self.assertHTMLEqual(p['age'].label_tag(), '<label for="id_age" class="required">Age</label>')
     1763
    17621764    def test_label_split_datetime_not_displayed(self):
    17631765        class EventForm(Form):
    17641766            happened_at = SplitDateTimeField(widget=widgets.SplitHiddenDateTimeWidget)
Back to Top