Skip to content

Commit

Permalink
Set the help_text id attribute to the field aria-describedby
Browse files Browse the repository at this point in the history
Otherwise, the rendered HTML does contains an `aria-describedby` that
references a nonexistent element.
  • Loading branch information
francoisfreitag committed Nov 5, 2024
1 parent d8f4541 commit 6ff4303
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/django_bootstrap5/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def get_help_html(self):
context={
"field": self.field,
"help_text": help_text,
"id_help_text": f"{self.field.auto_id}_helptext",
"layout": self.layout,
"show_help": self.show_help,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{% if help_text %}<div class="form-text">{{ help_text|safe }}</div>{% endif %}
{% if help_text %}<div id="{{ id_help_text }}" class="form-text">{{ help_text|safe }}</div>{% endif %}
14 changes: 13 additions & 1 deletion tests/test_bootstrap_field.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms

from .base import BootstrapTestCase
from .base import DJANGO_VERSION, BootstrapTestCase


class XssTestForm(forms.Form):
Expand All @@ -24,10 +24,22 @@ def test_illegal_field(self):
def test_show_help(self):
html = self.render("{% bootstrap_field form.subject %}", {"form": SubjectTestForm()})
self.assertIn("my_help_text", html)
if DJANGO_VERSION >= "5":
self.assertIn('aria-describedby="id_subject_helptext"', html)
self.assertIn('<div id="id_subject_helptext" class="form-text">my_help_text</div>', html)
self.assertNotIn("<i>my_help_text</i>", html)
html = self.render("{% bootstrap_field form.subject show_help=False %}", {"form": SubjectTestForm()})
self.assertNotIn("my_help_text", html)

if DJANGO_VERSION >= "5":

def test_helptext_id_matches_django(self):
form = SubjectTestForm({})
django_attrs = form["subject"].build_widget_attrs({})
aria_describedby = django_attrs["aria-describedby"]
html = self.render("{% bootstrap_field form.subject %}", {"form": form})
self.assertIn(f'<div id="{aria_describedby}" class="form-text">my_help_text</div>', html)

def test_placeholder(self):
html = self.render("{% bootstrap_field form.subject %}", {"form": SubjectTestForm()})
self.assertIn('type="text"', html)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bootstrap_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_exclude(self):
'<label class="form-label" for="id_required_text">Required text</label>'
'<input type="text" name="required_text" class="form-control"'
' placeholder="Required text" required id="id_required_text">'
'<div class="form-text"><i>required_text_help</i>'
'<div id="id_required_text_helptext" class="form-text"><i>required_text_help</i>'
"</div>"
),
)
Expand Down

0 comments on commit 6ff4303

Please sign in to comment.