Custom evaluation before submit of multi step form
-
Hi,
Trying to evaluate a multi-step form with some custom javascript when user hits Submit at the last step.
It works fine for single-step forms with:
event.preventDefault(); event.stopImmediatePropagation();But for multi-step forms the form submission doesn’t stop with this method and continues to send ajax request with POST.
Is there a way to intercept a multi-step form submission with javascript, then make some custom validation and if returned true continue with submission?I have tried to create MutationObserver to track the form steps with no success, please see below:
// Runs on jQuery( document ).ready function init(form){ // Forminator has totaly differnt behaviors for the forms that contains pagination // we need also implement differnt solution for them const totalSteps = form.querySelectorAll("div[data-step]").length if(totalSteps > 0 ){ const targetNode = document.querySelector('div.forminator-pagination-steps[role=tablist]') const config = { childList: true, } const observer = new MutationObserver(breakMenuAddedCallback) observer.observe(targetNode,config) form.addEventListener("click", e => paginationSubmitHandler(e, totalSteps)); } else { form.addEventListener("submit",submitHandler); } } function paginationSubmitHandler(event, totalSteps){ var target = event.target if( target.getAttribute('id') !='forminator-submit'){ return; } // Check if it is last page if(step == (totalSteps -1)) { submitHandler(event) } } function submitHandler(event){ event.preventDefault(); event.stopImmediatePropagation(); console.log("Reached hideLoader()"); hideLoader() showButtons("sbid-btn") hideButtons('open') setMessage("Välj hur du vill legitimera dig") hideSignOption(false) } var step = 0; function breakMenuAddedCallback(mutationList, observer){ mutationList.forEach(mutation => { if(mutation.addedNodes.length){ mutation.addedNodes.forEach(node => { // Observing the step buttons if( node.classList.contains('forminator-step')){ const config = {attributes : true } const observerInstance = new MutationObserver( (mutations, o) => { mutations.forEach(mutation => { if(mutation.attributeName == 'aria-selected'){ setInterval(() => { const page = document.querySelector('button.forminator-step[aria-selected=true]') if(page == null) { return } step = page.getAttribute('data-nav') }, 0) } }); }) observerInstance.observe(node, config) } }); } }); }
The topic ‘Custom evaluation before submit of multi step form’ is closed to new replies.