Skip to content

Commit

Permalink
Reference error ID from invalid field
Browse files Browse the repository at this point in the history
  • Loading branch information
Lubosky committed Aug 1, 2023
1 parent d0c3293 commit cc3dfb2
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions app/assets/javascripts/admin/form_answers.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"

"<div id='#{id}' class='alert alert-#{type}' data-controller='element-removal' role='alert' style='padding-top: 6px; padding-bottom: 6px; margin-bottom: 8px;'>
#{message}
Expand All @@ -452,8 +454,11 @@ buildBannerHtml = (message, type) ->
</div>"

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()
Expand Down

0 comments on commit cc3dfb2

Please sign in to comment.