Selective Store Form Fields Based on Submit Button

Introduction

In this article, you will learn how to selectively store form fields based on which button is clicked during form submission. This functionality is useful when you need to save certain data only under specific conditions, such as differentiating between saving a form draft and final submission. We will use a form example where the value for the Contact field should not be saved if the Save As Draft button is clicked.

How does it work?

To achieve this, you need to configure the form so that the Contact field is placed in its own section. This is because form section offers permission control.

This section will be controlled by a Bean Shell script that checks which button was used for submission. Specifically, you will:

  1. Configure the Form Section, place the Contact field in a separate section and set the section's permission to Bean Shell Script.
  2. Implement a script that detects whether the Save As Draft button was clicked. This script will control the permission to save the form fields within that section.

Here’s a sample Bean Shell script that determines if the Save As Draft button was clicked:

 //the following will return false (hash not empty against empty string) if "save as draft" button is clicked on form submission, othwerise, it will return true (form loading)
return "#requestParam.saveAsDraft?java#".equals("");

When Save As Draft button is clicked, the hash variable in the code above will be parsed as Save As Draft, thus making the equals function return false. In a permission plugin implementation, by returning false, the form fields inside its section will no longer be considered applicable for validation (executing its form validators) and saving (executing form store binder).

The same script can also be placed in the subform's form section permission, or in the subform's form permission too.

This means that the value c will only be replaced with the user's input value when the Complete button is hit; otherwise, it will stay as it is.

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