Skip to content

Commit dba5fc0

Browse files
committed
Fix help text and error IDs for aria-describedby
Django recently started using `aria-describedby` to associate form fields first with their help texts, and later also with error messages. In #716, django-bootstrap5 was adapted to also include those the necessary IDs in templates. `aria-describedby` for fields can have different values: - `"{field.auto_id}_helptext"` - `"{field.auto_id}_error"` - both of them - a user defined value that is left unchanged The approach in #716 was to use the value from `aria-describedby` for the ID of the help text. Unfortunately, this only works in the first case. So I propose to solve this the same way the django templates solve this and just hardcode the IDs.
1 parent a2afb62 commit dba5fc0

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

src/django_bootstrap5/renderers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,12 @@ def get_help_html(self):
395395
help_text = self.help_text or ""
396396
if help_text:
397397
widget_attrs = self.field.build_widget_attrs(self.widget.attrs)
398-
aria_describedby = widget_attrs.get("aria-describedby")
399398
return render_template_file(
400399
self.field_help_text_template,
401400
context={
402401
"field": self.field,
403402
"help_text": help_text,
404-
"id_help_text": aria_describedby,
403+
"id_help_text": f"{self.field.auto_id}_helptext",
405404
"layout": self.layout,
406405
"show_help": self.show_help,
407406
},
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
{% for text in field_errors %}
2-
<div class="invalid-feedback">{{ text }}</div>
3-
{% endfor %}
1+
{% if field_errors %}
2+
<div id="{{ field.auto_id }}_error">
3+
{% for text in field_errors %}
4+
<div class="invalid-feedback">{{ text }}</div>
5+
{% endfor %}
6+
</div>
7+
{% endif %}

tests/test_bootstrap_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_help_text_overridden_aria_describedby(self):
3737
form = SubjectTestForm()
3838
form.fields["subject"].widget.attrs["aria-describedby"] = "my_id"
3939
html = self.render("{% bootstrap_field form.subject %}", {"form": form})
40-
self.assertIn('<div id="my_id" class="form-text">my_help_text</div>', html)
40+
self.assertIn('<div id="id_subject_helptext" class="form-text">my_help_text</div>', html)
4141

4242
def test_placeholder(self):
4343
html = self.render("{% bootstrap_field form.subject %}", {"form": SubjectTestForm()})

tests/test_bootstrap_field_input_checkbox.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ def test_input_type_checkbox_is_invalid(self):
3737
'<div class="form-check">'
3838
'<input class="form-check-input is-invalid" id="id_test" name="test" required type="checkbox">'
3939
'<label class="form-check-label" for="id_test">Test</label>'
40+
'<div id="id_test_error">'
4041
'<div class="invalid-feedback">This field is required.</div>'
4142
"</div>"
4243
"</div>"
44+
"</div>"
4345
),
4446
)
4547

tests/test_bootstrap_field_input_text.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ def test_input_type_text(self):
4141
'<label class="form-label" for="id_test">Test</label>'
4242
'<input type="text" name="test"'
4343
' class="form-control is-invalid" placeholder="Test" required id="id_test">'
44+
'<div id="id_test_error">'
4445
'<div class="invalid-feedback">This field is required.</div>'
4546
"</div>"
47+
"</div>"
4648
),
4749
)
4850

@@ -195,9 +197,11 @@ def test_input_validation_failure(self):
195197
'<span class="input-group-text">foo</span>'
196198
'<input type="text" name="test" minlength="1" class="form-control'
197199
' is-invalid" placeholder="Test" required id="id_test">'
200+
'<div id="id_test_error">'
198201
'<div class="invalid-feedback">This field is required.</div>'
199202
"</div>"
200203
"</div>"
204+
"</div>"
201205
),
202206
)
203207

0 commit comments

Comments
 (0)