Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
English
In this proof of concept/showcase, we are creating new form by defining the form attributes and form fields in a Joget app itself.


Figure 1: Defining New Form

Figure 2: Form Created

You may study the bean shell code in the spreadsheet > store binder to learn how to programmatically create a new form.

Code Block
languagejava
linenumberstrue
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;

        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" +
                                    "}");
            //((JSONObject)formElement.get("properties")).put("validator", "");

        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");
}


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.

...