sutty/app/javascript/etc/validation.js

35 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2020-11-20 00:23:23 +00:00
document.addEventListener('turbolinks:load', () => {
// Al enviar el formulario del artículo, aplicar la validación
2020-11-20 00:23:23 +00:00
// localmente y actualizar los comentarios para lectores de pantalla.
document.querySelectorAll('form').forEach(form => {
form.addEventListener('submit', event => {
const invalid_help = form.querySelectorAll('.invalid-help')
const sending_help = form.querySelectorAll('.sending-help')
2020-11-20 00:23:23 +00:00
invalid_help.forEach(i => i.classList.add('d-none'))
sending_help.forEach(i => i.classList.add('d-none'))
2020-11-20 00:23:23 +00:00
form.querySelectorAll('[aria-invalid="true"]').forEach(aria => {
aria.setAttribute('aria-invalid', false)
aria.setAttribute('aria-describedby', aria.parentElement.querySelector('.feedback').id)
})
2018-06-15 22:13:18 +00:00
2020-11-20 00:23:23 +00:00
if (form.checkValidity() === false) {
event.preventDefault()
event.stopPropagation()
2020-11-20 00:23:23 +00:00
invalid_help.forEach(i => i.classList.remove('d-none'))
2018-06-15 22:13:18 +00:00
2020-11-20 00:23:23 +00:00
form.querySelectorAll(':invalid').forEach(invalid => {
invalid.setAttribute('aria-invalid', true)
invalid.setAttribute('aria-describedby', invalid.parentElement.querySelector('.invalid-feedback').id)
})
} else {
sending_help.forEach(i => i.classList.remove('d-none'))
}
2018-12-14 15:12:17 +00:00
2020-11-20 00:23:23 +00:00
form.classList.add('was-validated')
})
})
})