0
0
mirror of https://github.com/django/django.git synced 2024-11-21 19:09:18 +01:00

Refs #35706 -- Added an instance variable to track whether field errors exist

This commit is contained in:
sanjeevholla26 2024-09-09 18:32:06 +05:30
parent c9e98db1b3
commit 97ef622a78
3 changed files with 30 additions and 10 deletions

View File

@ -1940,6 +1940,7 @@ class ModelAdmin(BaseModelAdmin):
"inline_admin_formsets": inline_formsets,
"errors": helpers.AdminErrorList(form, formsets),
"preserved_filters": self.get_preserved_filters(request),
"field_errors_exist": form._field_errors_exist,
}
# Hide the "Save" and "Save and continue" buttons if "Save as New" was

View File

@ -25,27 +25,27 @@
{% block pretitle %}
{% if errors %}
<p class="errornote">
{% blocktranslate count counter=errors|length %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktranslate %}
</p>
{{ adminform.form.non_field_errors }}
<div class="errornote">
<p>The following fields have errors</p>
<div class="errornote" role="alert" aria-labelledby="error-summary-title" tabindex="-1">
<h2 id="error-summary-title">
{% blocktranslate %}There is a problem{% endblocktranslate %}
</h2>
<p>{{ adminform.form.non_field_errors }}</p>
{% if field_errors_exist %}
<p>The following fields have errors:</p>
<ul class="errorlist">
{% for fieldset in adminform %}
{% for line in fieldset %}
{% for field in line %}
{% if field.errors %}
<li>
<a href="#{{ field.field.id_for_label }}">{{ field.field.label }}</a>
<a href="#" onclick="focusField('{{ field.field.id_for_label }}'); event.preventDefault();">{{ field.field.label }}</a>
</li>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
</ul>
{% endif %}
</div>
{% endif %}
{% endblock %}
@ -85,6 +85,24 @@
{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
{% block admin_change_form_document_ready %}
{% if errors %}
<script>
document.addEventListener("DOMContentLoaded", function() {
const errorSummary = document.querySelector('.errornote[role="alert"]');
if (errorSummary) {
errorSummary.focus();
}
});
function focusField(fieldId) {
const fieldElement = document.getElementById(fieldId);
if (fieldElement) {
fieldElement.focus();
}
}
</script>
{% endif %}
<script id="django-admin-form-add-constants"
src="{% static 'admin/js/change_form.js' %}"
{% if adminform and add %}

View File

@ -94,7 +94,7 @@ class BaseForm(RenderableFormMixin):
self.label_suffix = label_suffix if label_suffix is not None else _(":")
self.empty_permitted = empty_permitted
self._errors = None # Stores the errors after clean() has been called.
self._field_errors_exist = False
# The base_fields class attribute is the *class-wide* definition of
# fields. Because a particular *instance* of the class might want to
# alter self.fields, we create self.fields here by copying base_fields.
@ -298,6 +298,7 @@ class BaseForm(RenderableFormMixin):
error_class="nonfield", renderer=self.renderer
)
else:
self._field_errors_exist = True
self._errors[field] = self.error_class(renderer=self.renderer)
self._errors[field].extend(error_list)
if field in self.cleaned_data: