Add Save as Draft with Validation in Form

Introduction

When one is in a flow, there are times one would like to simply validate the form data without actually completing the assignment.

  • Save as Draft button, by default, will only save the form data without validation.
  • Complete button, by default, will validate the form data and complete the assignment.
  • Save As Draft with Validation button, which is part of the article here, will validate and save the form data but not complete the assignment.

How does it work?

To implement a button that saves and validates form data without completing the assignment, follow these steps:

  1. To create a new button, open the form in the form builder.
  2. Create a new section at the bottom of the form.
  3. Edit the section's load data store, and set it to Bean Shell Form data store.
  4. In the Bean Shell Form data store, add the following code.

    import org.joget.apps.form.lib.SubmitButton;
    import org.joget.apps.form.lib.CustomHTML;
    import org.joget.apps.form.model.Column;
    import org.joget.apps.form.model.Element;
    import org.joget.apps.form.model.FormAction;
    import org.joget.apps.form.model.FormData;
    import org.joget.apps.form.model.Section;
    import org.joget.apps.form.service.FormUtil;
    import java.util.ArrayList;
    import java.util.Collection;
    
    Collection<FormAction> formActions = new ArrayList<>();
    String saveButtonLabel = "Save As Draft with Validation";
    Element saveButton = new SubmitButton();
    saveButton.setProperty(FormUtil.PROPERTY_ID, "customSaveAsDraft");
    saveButton.setProperty("label", saveButtonLabel);
    formActions.add(saveButton);
    
    Section section = element;
    ArrayList<Column> columns = (ArrayList<Column>) section.getChildren();
    
    Column column = columns.get(0);
    column.setProperty("horizontal", "true");
    column.setChildren(formActions);
    
    // Add custom HTML to correct the layout issue
    Element html = new CustomHTML();
    String script = "<script>$(document).ready(function(){";
    script += "$('#" + section.getPropertyString("id") + "').find('.form-cell').prependTo('#section-actions .form-column');";
    script += "$('#" + section.getPropertyString("id") + "').remove();";
    script += "});</script>";
    html.setProperty("id", "button_layout_fixes");
    html.setProperty("label", "");
    html.setProperty("value", script);
    formActions.add(html);
    
    return null;

For advanced form customization, learn how to Temporarily Disable Form Validation on Form Button Click. Discover how this technique can provide flexibility in the data entry process and enhance the user experience.

Created by Julieth Last modified by Aadrian on Dec 13, 2024