Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
imp: open accordion on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kobecuppens1997 committed Mar 25, 2024
1 parent ce63854 commit 193d033
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions aldryn_forms/templates/aldryn_forms/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,34 @@
</script>
{% endaddtoblock %}
{% endif %}
<script>
(function() {
const form = document.querySelector('#cms-form');
const isInsideAccordion = form.querySelector('.accordion') !== null;
if (isInsideAccordion) {
document.querySelector('#cms-form [type="submit"]').addEventListener('click', (event) => {
let isValid = true;
const fields = form.querySelectorAll('input');
fields.forEach(field => {
if (!field.validity.valid || field.classList.contains('is-invalid')) {
isValid = false;
const accordionItem = field.closest('.accordion-item');
if (accordionItem) {
const collapseElement = accordionItem.querySelector('.collapse');
if (collapseElement && !collapseElement.classList.contains('show')) {
$(collapseElement).collapse('show');
}
}
field.focus();
}
});

if (isValid) {
console.log('Form is valid. Submitting...');
} else {
event.preventDefault();
}
});
}
})();
</script>

0 comments on commit 193d033

Please sign in to comment.