Temporarily Disable Form Validation for New Records

Introduction

In some cases, you may want to disable form validation during record creation while keeping it active for record updates. This can be achieved by using a Bean Shell Load Binder. By adding a dummy section to your form and using a script, you can ensure that form validation is bypassed when the form is in add mode (such as when the primary key does not exist).

How does it work?

Follow these detailed steps to implement this functionality in your Joget forms:

  1. Add an empty or dummy section to your form. This section will be used to execute the script.
  2. Change the section's load Data Store to a Bean Shell Load Data Store and insert the following script. Use the Bean Shell script to disable form validation when the primary key is null (in add mode).
    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) {
        if (primaryKey == null) {
            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);
To learn more, download the sample application below.
Created by Julieth Last modified by Aadrian on Dec 13, 2024