From 416a8580232075d569cf3c386a97067884c67a60 Mon Sep 17 00:00:00 2001 From: Anubhav Joshi Date: Mon, 3 Mar 2014 23:29:03 +0530 Subject: [PATCH] Fixed #17922 -- Added required_css_class to form label. Thanks hanson2010 for the suggestion. --- django/forms/forms.py | 6 +++++ docs/ref/forms/api.txt | 19 ++++++++++++--- docs/releases/1.8.txt | 4 ++++ tests/forms_tests/tests/test_forms.py | 33 +++++++++++++++++++-------- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/django/forms/forms.py b/django/forms/forms.py index 97a344e9fc..a0dcaab531 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -621,6 +621,12 @@ class BoundField(object): id_for_label = widget.id_for_label(id_) if id_for_label: attrs = dict(attrs or {}, **{'for': id_for_label}) + if self.field.required and hasattr(self.form, 'required_css_class'): + attrs = attrs or {} + if 'class' in attrs: + attrs['class'] += ' ' + self.form.required_css_class + else: + attrs['class'] = self.form.required_css_class attrs = flatatt(attrs) if attrs else '' contents = format_html('{1}', attrs, contents) else: diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index cb6cf7a51b..53e47ed0c2 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -511,10 +511,19 @@ classes, as needed. The HTML will look something like:: >>> f = ContactForm(data) >>> print(f.as_table()) - ... - ... - ... + ... + ... + ...