• Resolved antonborgstrom

    (@antonborgstrom)


    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)
    				  }
    			  });
    		  }
    	  });
      }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @antonborgstrom

    I hope you’re well today!

    Most “custom validation” solution that we looked into so far, they usually involved some PHP anyway to hook to Forminator code. Is there any reason to specifically use only JS for that?

    If not, maybe this custom script would be helpful for you then? Take a look, please

    https://gist.github.com/wpmudev-sls/9a2dc6ced8ba2a5c8777b534dfcae486

    You should be able to define various validation rules of your own (or even extend it if needed) in the part right below the “// set your form validation list” comment in code.

    Would this work for you?

    Kind regards,
    Adam

    Thread Starter antonborgstrom

    (@antonborgstrom)

    Hi Adam,

    Thanks for your reply!
    It’s not so much a custom validation on a form field, it’s a third party E-signature validation for the entire form that needs to be passed before submission.

    Instead of using the built in E-Signature component, when the user hits Submit the idea is that he/she needs to authenticate and sign the form with the e-sign solution Swedish BankID (which is required for the form contract to be legally binding).

    The E-signature process needs to be at the very end, when forminator has validated all required fields. Perhaps this could be handled in a hook in the callback instead of in javascript, if so I’d be very grateful if you know how this could be done.

    Best regards, Anton

    Thread Starter antonborgstrom

    (@antonborgstrom)

    Turns out the jQuery equivalent of the submission-handler can properly intercept the Forminator submit.

    So this works even for multi step forms:

    $( '#forminator-module-' + form_id ).submit( function( event ) {
            event.preventDefault();
      	event.stopImmediatePropagation();
    });

    Thanks for the support anyways!

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @antonborgstrom

    Nice find!

    Thanks for sharing it, I’m sure we and/or other user will find it useful.

    I’ll mark this ticket as resolved then but of course feel free to re-open if needed.

    Have a great day,
    Adam

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom evaluation before submit of multi step form’ is closed to new replies.