Temporarily Disable Form Validation on Button Click

Introduction

Sometimes, you may need to bypass form validation, such as when rejecting a request. This can be achieved by using a Bean Shell Load Data Store. Adding a dummy section to your form and using a script can remove form validation when a specific button is clicked.

There are two methods available to implement this functionality. Below, we will explore each method, providing step-by-step guidance to help you effectively integrate them into your Joget forms.

Method 1: bypass validation by clicking on the reject button 

This method may not be required if you are using the Process Enhancement Plugin). There is a skip validation checkbox under Map Activities to Forms - More Settings

To bypass validation by clicking on the reject button, follow these steps:

  1. Add an empty or dummy section to your form. 
  2. Change the section's load Data Store to a Bean Shell Load Data Store and insert the following script.
    import org.joget.apps.app.service.AppUtil;
    import org.joget.apps.form.service.FormUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.Form;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.model.FormRowSet;
    
    public FormRowSet load(Element element, String primaryKey, FormData formData) {
        // Change this condition to suit your needs
        if ("#requestParam.process_reject#".equalsIgnoreCase("Reject")) {
            Form form = FormUtil.findRootForm(element);
            removeValidator(form, formData);
        }
        return null;
    }
    
    public void removeValidator(Element e, FormData formData) {
        e.setValidator(null);
    
        Collection children = e.getChildren(formData);
        if (children != null) {
            for (Element child : children) {
                removeValidator(child, formData);
            }
        }
    }
    
    // Call load method with injected variable
    return load(element, primaryKey, formData);

Method 2: bypass validation using a form button

To bypass validation using a button inside the form, follow these steps:

  1. Add an empty or dummy section to your form. 
  2. Change the section's load Data Store to a Bean Shell Load Data Store and insert the following script, replacing "disable_validation" with the ID of the select box form element and "Disable" with the option value that will disable validation.

    import org.joget.apps.app.service.AppUtil;
    import org.joget.apps.form.service.FormUtil;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.Form;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.model.FormRowSet;
    
    public FormRowSet load(Element element, String primaryKey, FormData formData) {
        // Get disable_validation value from form data object
        String disableValidatorID = "disable_validation";
        Form form = FormUtil.findRootForm(element);
        Element disableValidatorElement = FormUtil.findElement(disableValidatorID, form, formData);
        
        if (disableValidatorElement != null) {
            // Get value of disable validator
            String[] disableValidatorValues = FormUtil.getElementPropertyValues(disableValidatorElement, formData);
            for (int i = 0; i < disableValidatorValues.length; i++) {
                System.out.println("Toggle Button Value " + i + ": " + disableValidatorValues[i]);
                // Remove validator if checkbox value = Disable
                if (disableValidatorValues[i].equalsIgnoreCase("Disable")) {
                    removeValidator(form, formData);
                    break;
                }
            }
        }
        return null;
    }
    
    public void removeValidator(Element e, FormData formData) {
        e.setValidator(null);
        
        Collection children = e.getChildren(formData);
        if (children != null) {
            for (Element child : children) {
                removeValidator(child, formData);
            }
        }
    }
    
    // Call load method with injected variable
    return load(element, primaryKey, formData);

Download sample app

Download the demo app for Method 2:
Created by Julieth Last modified by Nik Nufayl on Jan 06, 2025