From ec65484d4c29e3e6815fcf76e816213670405639 Mon Sep 17 00:00:00 2001 From: Rachid Mrad Date: Mon, 9 Dec 2024 18:27:09 -0500 Subject: [PATCH] Set a correct aria-describedby when sublabel and error --- .../templates/django/forms/widgets/input.html | 1 - src/registrar/templatetags/field_helpers.py | 11 ++++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/registrar/templates/django/forms/widgets/input.html b/src/registrar/templates/django/forms/widgets/input.html index e7b43655d..91f5b20ea 100644 --- a/src/registrar/templates/django/forms/widgets/input.html +++ b/src/registrar/templates/django/forms/widgets/input.html @@ -5,6 +5,5 @@ class="{{ uswds_input_class }}{% if classes %} {{ classes }}{% endif %}" {% if widget.value != None %}value="{{ widget.value|stringformat:'s' }}"{% endif %} {% if aria_label %}aria-label="{{ aria_label }} {{ label }}"{% endif %} - {% if sublabel_text %}aria-describedby="{{ widget.attrs.id }}__sublabel"{% endif %} {% include "django/forms/widgets/attrs.html" %} /> diff --git a/src/registrar/templatetags/field_helpers.py b/src/registrar/templatetags/field_helpers.py index 8a80a75b9..d0f85a231 100644 --- a/src/registrar/templatetags/field_helpers.py +++ b/src/registrar/templatetags/field_helpers.py @@ -57,6 +57,7 @@ def input_with_errors(context, field=None): # noqa: C901 legend_classes = [] group_classes = [] aria_labels = [] + sublabel_text = [] # this will be converted to an attribute string described_by = [] @@ -103,6 +104,9 @@ def input_with_errors(context, field=None): # noqa: C901 elif key == "add_aria_label": aria_labels.append(value) + elif key == "sublabel_text": + sublabel_text.append(value) + attrs["id"] = field.auto_id # do some work for various edge cases @@ -152,11 +156,16 @@ def input_with_errors(context, field=None): # noqa: C901 if group_classes: context["group_classes"] = " ".join(group_classes) + # We handle sublabel_text here instead of directy in the template to avoid conflicts + if sublabel_text: + sublabel_div_id = f"{attrs['id']}__sublabel" + described_by.insert(0, sublabel_div_id) + if described_by: # ensure we don't overwrite existing attribute value if "aria-describedby" in attrs: described_by.append(attrs["aria-describedby"]) - attrs["aria_describedby"] = " ".join(described_by) + attrs["aria-describedby"] = " ".join(described_by) if aria_labels: context["aria_label"] = " ".join(aria_labels)