summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorDavid Smith <smithdc@gmail.com>2023-11-18 20:36:45 +0000
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-12-05 10:24:39 +0100
commitedd74c3417fa3a0b29295012ff31dbe44843303c (patch)
treef2992dd39bea49bcd891b63e9ecd691b25617dd7 /django/forms
parent28f81a10190de9aa00925156c0005f6c787afeb3 (diff)
Refs #32819 -- Added id to ErrorList class and template.
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/forms.py5
-rw-r--r--django/forms/jinja2/django/forms/errors/list/ul.html2
-rw-r--r--django/forms/templates/django/forms/errors/list/ul.html2
-rw-r--r--django/forms/utils.py3
4 files changed, 8 insertions, 4 deletions
diff --git a/django/forms/forms.py b/django/forms/forms.py
index 452f554e1e..549a3adf6f 100644
--- a/django/forms/forms.py
+++ b/django/forms/forms.py
@@ -298,7 +298,10 @@ class BaseForm(RenderableFormMixin):
error_class="nonfield", renderer=self.renderer
)
else:
- self._errors[field] = self.error_class(renderer=self.renderer)
+ self._errors[field] = self.error_class(
+ renderer=self.renderer,
+ field_id=self[field].auto_id,
+ )
self._errors[field].extend(error_list)
if field in self.cleaned_data:
del self.cleaned_data[field]
diff --git a/django/forms/jinja2/django/forms/errors/list/ul.html b/django/forms/jinja2/django/forms/errors/list/ul.html
index 752f7c2c8b..59528efccc 100644
--- a/django/forms/jinja2/django/forms/errors/list/ul.html
+++ b/django/forms/jinja2/django/forms/errors/list/ul.html
@@ -1 +1 @@
-{% if errors %}<ul class="{{ error_class }}">{% for error in errors %}<li>{{ error }}</li>{% endfor %}</ul>{% endif %}
+{% if errors %}<ul class="{{ error_class }}"{% if errors.field_id %} id="{{ errors.field_id }}_error"{% endif %}>{% for error in errors %}<li>{{ error }}</li>{% endfor %}</ul>{% endif %}
diff --git a/django/forms/templates/django/forms/errors/list/ul.html b/django/forms/templates/django/forms/errors/list/ul.html
index 57b34ccb88..c28ce8af67 100644
--- a/django/forms/templates/django/forms/errors/list/ul.html
+++ b/django/forms/templates/django/forms/errors/list/ul.html
@@ -1 +1 @@
-{% if errors %}<ul class="{{ error_class }}">{% for error in errors %}<li>{{ error }}</li>{% endfor %}</ul>{% endif %} \ No newline at end of file
+{% if errors %}<ul class="{{ error_class }}"{% if errors.field_id %} id="{{ errors.field_id }}_error"{% endif %}>{% for error in errors %}<li>{{ error }}</li>{% endfor %}</ul>{% endif %} \ No newline at end of file
diff --git a/django/forms/utils.py b/django/forms/utils.py
index f4fbf3e241..d24711d1a0 100644
--- a/django/forms/utils.py
+++ b/django/forms/utils.py
@@ -147,7 +147,7 @@ class ErrorList(UserList, list, RenderableErrorMixin):
template_name_text = "django/forms/errors/list/text.txt"
template_name_ul = "django/forms/errors/list/ul.html"
- def __init__(self, initlist=None, error_class=None, renderer=None):
+ def __init__(self, initlist=None, error_class=None, renderer=None, field_id=None):
super().__init__(initlist)
if error_class is None:
@@ -155,6 +155,7 @@ class ErrorList(UserList, list, RenderableErrorMixin):
else:
self.error_class = "errorlist {}".format(error_class)
self.renderer = renderer or get_default_renderer()
+ self.field_id = field_id
def as_data(self):
return ValidationError(self.data).error_list