From cc3dfb2c24c5b8deb56dde06718c5ad8edad446a Mon Sep 17 00:00:00 2001 From: Lubos Hricak Date: Tue, 1 Aug 2023 00:47:03 -0300 Subject: [PATCH] Reference error ID from invalid field --- .../javascripts/admin/form_answers.js.coffee | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/admin/form_answers.js.coffee b/app/assets/javascripts/admin/form_answers.js.coffee index db113cfc8e..4e681d9c7a 100644 --- a/app/assets/javascripts/admin/form_answers.js.coffee +++ b/app/assets/javascripts/admin/form_answers.js.coffee @@ -439,10 +439,12 @@ showErrorForInvalidField = (field, values) -> if container values.forEach (message) -> if (!container.querySelector('.alert')) - container.insertAdjacentHTML('afterbegin', buildBannerHtml(message, 'danger')) + id = "alert__#{String(Math.random()).slice(2, -1)}" + field.setAttribute('aria-errormessage', id) + container.insertAdjacentHTML('afterbegin', buildBannerHtml(message, 'danger', id)) -buildBannerHtml = (message, type) -> - id = "alert__#{String(Math.random()).slice(2, -1)}" +buildBannerHtml = (message, type, identifier = null) -> + id = identifier || "alert__#{String(Math.random()).slice(2, -1)}" "" removeExistingErrorMessages = (element) -> - element.querySelectorAll('.field-with-errors').forEach (banner) -> - banner.classList.remove('field-with-errors') + element.querySelectorAll('.field-with-errors').forEach (el) -> + el.classList.remove('field-with-errors') + + element.querySelectorAll('[aria-errormessage]').forEach (el) -> + el.removeAttribute('aria-errormessage') element.querySelectorAll('.alert').forEach (banner) -> banner.remove()