mirror of
https://github.com/django/django.git
synced 2024-11-29 14:46:18 +01:00
Fixed #17922 -- Added required_css_class to form label.
Thanks hanson2010 for the suggestion.
This commit is contained in:
parent
2ec82c7387
commit
416a858023
@ -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('<label{0}>{1}</label>', attrs, contents)
|
||||
else:
|
||||
|
@ -511,10 +511,19 @@ classes, as needed. The HTML will look something like::
|
||||
|
||||
>>> f = ContactForm(data)
|
||||
>>> print(f.as_table())
|
||||
<tr class="required"><th><label for="id_subject">Subject:</label> ...
|
||||
<tr class="required"><th><label for="id_message">Message:</label> ...
|
||||
<tr class="required error"><th><label for="id_sender">Sender:</label> ...
|
||||
<tr class="required"><th><label class="required" for="id_subject">Subject:</label> ...
|
||||
<tr class="required"><th><label class="required" for="id_message">Message:</label> ...
|
||||
<tr class="required error"><th><label class="required" for="id_sender">Sender:</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:
|
||||
|
||||
@ -799,6 +808,10 @@ additional attributes for the ``<label>`` tag.
|
||||
template, you could write a custom filter to allow passing parameters to
|
||||
``label_tag``.
|
||||
|
||||
.. versionchanged:: 1.8
|
||||
|
||||
The label includes :attr:`~Form.required_css_class` if applicable.
|
||||
|
||||
.. method:: BoundField.css_classes()
|
||||
|
||||
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
|
||||
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
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
@ -1814,34 +1814,49 @@ class FormsTestCase(TestCase):
|
||||
p.error_css_class = 'error'
|
||||
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>
|
||||
<li class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool">
|
||||
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 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="2">Yes</option>
|
||||
<option value="3">No</option>
|
||||
</select></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>
|
||||
<p class="required error"><label 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 error"><label class="required" for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
|
||||
<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="2">Yes</option>
|
||||
<option value="3">No</option>
|
||||
</select></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>
|
||||
<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>
|
||||
<tr class="required"><th><label for="id_is_cool">Is cool:</label></th><td><select name="is_cool" id="id_is_cool">
|
||||
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 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="2">Yes</option>
|
||||
<option value="3">No</option>
|
||||
</select></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):
|
||||
class EventForm(Form):
|
||||
|
Loading…
Reference in New Issue
Block a user