Create New Form Programmatically

You learn how to create a new form by defining the form attributes and form fields directly within a Joget app. This approach streamlines the form creation process by programmatically generating form definitions.

By defining form attributes and fields in code, you can quickly generate complex forms without manually dragging and dropping each field. This method is particularly useful for large forms or dynamically generating forms based on external data.

Instructions

Define New Form and set up the form attributes and fields as needed.

You can study the Bean Shell code in the spreadsheet > Data Store to learn how to create a new form programmatically. Here is an example of the script used:

import java.util.Collection;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.model.FormDefinition;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.commons.util.LogUtil;
import org.json.JSONArray;
import org.json.JSONObject;

String formDefId      = formData.getRequestParameter("form_def_id");
String formName       = formData.getRequestParameter("form_name");
String formTableName  = formData.getRequestParameter("form_table_name");

try {
    JSONArray fields = new JSONArray();

    for (FormRow row : rows) {
        String fieldId = row.getProperty("field_id");
        String fieldLabel = row.getProperty("field_label");
        String className = row.getProperty("field_type");

        JSONObject formElement = new JSONObject("{\n" +
            "    \"className\" : \"" + className + "\",\n" +
            "    \"properties\": {\n" +
            "        \"controlField\": \"\",\n" +
            "        \"id\": \"" + fieldId + "\",\n" +
            "        \"label\": \"" + fieldLabel + "\",\n" +
            "        \"permissionHidden\": \"\",\n" +
            "        \"readonly\": \"\",\n" +
            "        \"readonlyLabel\": \"\",\n" +
            "        \"size\": \"\",\n" +
            "        \"validator\": {\n" +
            "            \"className\": \"\",\n" +
            "            \"properties\": {}\n" +
            "        },\n" +
            "        \"value\": \"\",\n" +
            "        \"workflowVariable\": \"\"\n" +
            "    }\n" +
            "}");

        fields.put(formElement);
    }

    JSONObject form = new JSONObject("{\n" +
        "    \"className\": \"org.joget.apps.form.model.Form\",\n" +
        "    \"elements\": [{\n" +
        "        \"className\": \"org.joget.apps.form.model.Section\",\n" +
        "        \"elements\": [{\n" +
        "            \"className\": \"org.joget.apps.form.model.Column\",\n" +
        "            \"elements\":" + fields.toString() + ",\n" +
        "            \"properties\": {\"width\": \"100%\"}\n" +
        "        }],\n" +
        "        \"properties\": {\n" +
        "            \"id\": \"section1\",\n" +
        "            \"label\": \"Section\"\n" +
        "        }\n" +
        "    }],\n" +
        "    \"properties\": {\n" +
        "        \"description\": \"\",\n" +
        "        \"id\": \"" + formDefId + "\",\n" +
        "        \"loadBinder\": {\n" +
        "            \"className\": \"org.joget.apps.form.lib.WorkflowFormBinder\",\n" +
        "            \"properties\": {}\n" +
        "        },\n" +
        "        \"name\": \"" + formName + "\",\n" +
        "        \"noPermissionMessage\": \"\",\n" +
        "        \"permission\": {\n" +
        "            \"className\": \"\",\n" +
        "            \"properties\": {}\n" +
        "        },\n" +
        "        \"postProcessor\": null,\n" +
        "        \"postProcessorRunOn\": \"create\",\n" +
        "        \"storeBinder\": {\n" +
        "            \"className\": \"org.joget.apps.form.lib.WorkflowFormBinder\",\n" +
        "            \"properties\": {}\n" +
        "        },\n" +
        "        \"tableName\": \"" + formTableName + "\"\n" +
        "    }\n" +
        "}");

    FormDefinition formDefinition = new FormDefinition();
    formDefinition.setJson(form.toString());

    formDefinition.setId(formDefId);
    formDefinition.setName(formName);
    formDefinition.setTableName(formTableName);

    AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
    AppDefinition appDef = AppUtil.getCurrentAppDefinition();
    Collection errors = appService.createFormDefinition(appDef, formDefinition);
} catch (Exception ex) {
    LogUtil.error("formCreatorApp", ex, "Cannot Create Form");
}

Customization

  • Modify the connection string if you need to perform the table lookup in another database.
  • Depending on your needs, you can change the form elements from TextField to other types.

To try out this app, please download and install the plugin from https://github.com/jogetoss/multi-store-binders/releases before importing the app into your copy of Joget.

Download a demo application for Create New Form Programmatically in the link below.
.
Created by Julieth Last modified by Aadrian on Dec 13, 2024