mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #17922 -- Added required_css_class to form label.
Thanks hanson2010 for the suggestion.
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							2ec82c7387
						
					
				
				
					commit
					416a858023
				
			| @@ -621,6 +621,12 @@ class BoundField(object): | |||||||
|             id_for_label = widget.id_for_label(id_) |             id_for_label = widget.id_for_label(id_) | ||||||
|             if id_for_label: |             if id_for_label: | ||||||
|                 attrs = dict(attrs or {}, **{'for': 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 '' |             attrs = flatatt(attrs) if attrs else '' | ||||||
|             contents = format_html('<label{0}>{1}</label>', attrs, contents) |             contents = format_html('<label{0}>{1}</label>', attrs, contents) | ||||||
|         else: |         else: | ||||||
|   | |||||||
| @@ -511,10 +511,19 @@ classes, as needed. The HTML will look something like:: | |||||||
|  |  | ||||||
|     >>> f = ContactForm(data) |     >>> f = ContactForm(data) | ||||||
|     >>> print(f.as_table()) |     >>> print(f.as_table()) | ||||||
|     <tr class="required"><th><label for="id_subject">Subject:</label>    ... |     <tr class="required"><th><label class="required" for="id_subject">Subject:</label>    ... | ||||||
|     <tr class="required"><th><label for="id_message">Message:</label>    ... |     <tr class="required"><th><label class="required" for="id_message">Message:</label>    ... | ||||||
|     <tr class="required error"><th><label for="id_sender">Sender:</label>      ... |     <tr class="required error"><th><label class="required" for="id_sender">Sender:</label>      ... | ||||||
|     <tr><th><label for="id_cc_myself">Cc myself:<label> ... |     <tr><th><label for="id_cc_myself">Cc myself:<label> ... | ||||||
|  |     >>> f['subject'].label_tag() | ||||||
|  |     <label class="required" for="id_subject">Subject:</label> | ||||||
|  |     >>> f['subject'].label_tag(attrs={'class': 'foo'}) | ||||||
|  |     <label for="id_subject" class="foo required">Subject:</label> | ||||||
|  |  | ||||||
|  | .. versionchanged:: 1.8 | ||||||
|  |  | ||||||
|  |     The ``required_css_class`` will also be added to the ``<label>`` tag as | ||||||
|  |     seen above. | ||||||
|  |  | ||||||
| .. _ref-forms-api-configuring-label: | .. _ref-forms-api-configuring-label: | ||||||
|  |  | ||||||
| @@ -799,6 +808,10 @@ additional attributes for the ``<label>`` tag. | |||||||
|     template, you could write a custom filter to allow passing parameters to |     template, you could write a custom filter to allow passing parameters to | ||||||
|     ``label_tag``. |     ``label_tag``. | ||||||
|  |  | ||||||
|  | .. versionchanged:: 1.8 | ||||||
|  |  | ||||||
|  |     The label includes :attr:`~Form.required_css_class` if applicable. | ||||||
|  |  | ||||||
| .. method:: BoundField.css_classes() | .. method:: BoundField.css_classes() | ||||||
|  |  | ||||||
| When you use Django's rendering shortcuts, CSS classes are used to | When you use Django's rendering shortcuts, CSS classes are used to | ||||||
|   | |||||||
| @@ -112,6 +112,10 @@ Forms | |||||||
| * The new :meth:`~django.forms.Form.has_error()` method allows checking | * The new :meth:`~django.forms.Form.has_error()` method allows checking | ||||||
|   if a specific error has happened. |   if a specific error has happened. | ||||||
|  |  | ||||||
|  | * If :attr:`~django.forms.Form.required_css_class` is defined on a form, then | ||||||
|  |   the ``<label>`` tags for required fields will have this class present in its | ||||||
|  |   attributes. | ||||||
|  |  | ||||||
| Internationalization | Internationalization | ||||||
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^ | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1814,34 +1814,49 @@ class FormsTestCase(TestCase): | |||||||
|         p.error_css_class = 'error' |         p.error_css_class = 'error' | ||||||
|         p.required_css_class = 'required' |         p.required_css_class = 'required' | ||||||
|  |  | ||||||
|         self.assertHTMLEqual(p.as_ul(), """<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li> |         self.assertHTMLEqual(p.as_ul(), """<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label class="required" for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li> | ||||||
| <li class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool"> | <li class="required"><label class="required" for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool"> | ||||||
| <option value="1" selected="selected">Unknown</option> | <option value="1" selected="selected">Unknown</option> | ||||||
| <option value="2">Yes</option> | <option value="2">Yes</option> | ||||||
| <option value="3">No</option> | <option value="3">No</option> | ||||||
| </select></li> | </select></li> | ||||||
| <li><label for="id_email">Email:</label> <input type="email" name="email" id="id_email" /></li> | <li><label for="id_email">Email:</label> <input type="email" name="email" id="id_email" /></li> | ||||||
| <li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_age">Age:</label> <input type="number" name="age" id="id_age" /></li>""") | <li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label class="required" for="id_age">Age:</label> <input type="number" name="age" id="id_age" /></li>""") | ||||||
|  |  | ||||||
|         self.assertHTMLEqual(p.as_p(), """<ul class="errorlist"><li>This field is required.</li></ul> |         self.assertHTMLEqual(p.as_p(), """<ul class="errorlist"><li>This field is required.</li></ul> | ||||||
| <p class="required error"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p> | <p class="required error"><label class="required" for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p> | ||||||
| <p class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool"> | <p class="required"><label class="required" for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool"> | ||||||
| <option value="1" selected="selected">Unknown</option> | <option value="1" selected="selected">Unknown</option> | ||||||
| <option value="2">Yes</option> | <option value="2">Yes</option> | ||||||
| <option value="3">No</option> | <option value="3">No</option> | ||||||
| </select></p> | </select></p> | ||||||
| <p><label for="id_email">Email:</label> <input type="email" name="email" id="id_email" /></p> | <p><label for="id_email">Email:</label> <input type="email" name="email" id="id_email" /></p> | ||||||
| <ul class="errorlist"><li>This field is required.</li></ul> | <ul class="errorlist"><li>This field is required.</li></ul> | ||||||
| <p class="required error"><label for="id_age">Age:</label> <input type="number" name="age" id="id_age" /></p>""") | <p class="required error"><label class="required" for="id_age">Age:</label> <input type="number" name="age" id="id_age" /></p>""") | ||||||
|  |  | ||||||
|         self.assertHTMLEqual(p.as_table(), """<tr class="required error"><th><label for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="name" id="id_name" /></td></tr> |         self.assertHTMLEqual(p.as_table(), """<tr class="required error"><th><label class="required" for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="name" id="id_name" /></td></tr> | ||||||
| <tr class="required"><th><label for="id_is_cool">Is cool:</label></th><td><select name="is_cool" id="id_is_cool"> | <tr class="required"><th><label class="required" for="id_is_cool">Is cool:</label></th><td><select name="is_cool" id="id_is_cool"> | ||||||
| <option value="1" selected="selected">Unknown</option> | <option value="1" selected="selected">Unknown</option> | ||||||
| <option value="2">Yes</option> | <option value="2">Yes</option> | ||||||
| <option value="3">No</option> | <option value="3">No</option> | ||||||
| </select></td></tr> | </select></td></tr> | ||||||
| <tr><th><label for="id_email">Email:</label></th><td><input type="email" name="email" id="id_email" /></td></tr> | <tr><th><label for="id_email">Email:</label></th><td><input type="email" name="email" id="id_email" /></td></tr> | ||||||
| <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="number" name="age" id="id_age" /></td></tr>""") | <tr class="required error"><th><label class="required" for="id_age">Age:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="number" name="age" id="id_age" /></td></tr>""") | ||||||
|  |  | ||||||
|  |     def test_label_has_required_css_class(self): | ||||||
|  |         """ | ||||||
|  |         #17922 - required_css_class is added to the label_tag() of required fields. | ||||||
|  |         """ | ||||||
|  |         class SomeForm(Form): | ||||||
|  |             required_css_class = 'required' | ||||||
|  |             field = CharField(max_length=10) | ||||||
|  |             field2 = IntegerField(required=False) | ||||||
|  |  | ||||||
|  |         f = SomeForm({'field': 'test'}) | ||||||
|  |         self.assertHTMLEqual(f['field'].label_tag(), '<label for="id_field" class="required">Field:</label>') | ||||||
|  |         self.assertHTMLEqual(f['field'].label_tag(attrs={'class': 'foo'}), | ||||||
|  |             '<label for="id_field" class="foo required">Field:</label>') | ||||||
|  |         self.assertHTMLEqual(f['field2'].label_tag(), '<label for="id_field2">Field2:</label>') | ||||||
|  |  | ||||||
|     def test_label_split_datetime_not_displayed(self): |     def test_label_split_datetime_not_displayed(self): | ||||||
|         class EventForm(Form): |         class EventForm(Form): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user