Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

...

Do some calculation before storing the data using Workflow Form Binder.

Code Block
languagejava
 

 

Use as Form Validator

Injected Variables:

...

import java.text.DecimalFormat;
import org.joget.apps.

...

app.

...

service.AppUtil;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData

...

Expected Outcome:

    • A boolean value to indicate the validation pass or fail.

Samples:

Compare the submitted value with the value of another field.

Code Block
languagejava
import java.util.Arrays;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.ElementFormRow;
import org.joget.apps.form.model.FormFormRowSet;
import org.joget.apps.form.model.FormDataFormStoreBinder;
import org.joget.appsplugin.formbase.service.FormUtilPluginManager;

public booleanFormRowSet validatestore(Element element, FormDataFormRowSet formDatarows, String[] valuesFormData formData) {
    boolean result = true;

    //getcheck the fieldrows 1is valuenot fromempty formbefore datastore objectit
    Stringif field1Id(rows != "field1";
 null && !rows.isEmpty()) {
     Form form = FormUtil.findRootForm(element);   //Get the submitted data
    Element field1 = FormUtil.findElement(field1Id, form, formData);
 FormRow row = rows.get(0);
    if (field1 != null) {
        //getString valueunitPrice of field 1= row.getProperty("unitPrice");
        String[] compareValuesunit = FormUtilrow.getElementPropertyValues(field1, formDatagetProperty("unit");

        //compare
  the value of field 2 and fielddouble 1total are= equals0;
        if (!Arrays.equals(values, compareValues)) {
try {
            double   Stringprice id = FormUtilDouble.getElementParameterNameparseDouble(elementunitPrice);
            formData.addFormError(id, "Value not equal!!!!"int unit = Integer.parseInt(unit);
            result
 = false;
        } 
  total = }price else* {unit;
        //ignore} ifcatch the(Exception field 1 not existe) {}
    }

    return result; 
}

//call validate method with injected variable
return validate(element, formData, values);

Use as Form Multi Row Load Binder

Injected Variables:

...

 
        //format the total to 2 decimal
        DecimalFormat df = new DecimalFormat("#.00");
        row.setProperty("total", df.format(total));
    
        //Reuse Workflow Form Binder to store data
        PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
        FormStoreBinder binder = (FormStoreBinder) pluginManager.getPlugin("org.joget.apps.form.

...

lib.

...

WorkflowFormBinder");
        binder.store(element, rows, formData);
    }
    
    return rows;
}

//call store method with injected variable
return store(element, rows, formData);

Use as Form Validator

Injected Variables:

    • element - Element that this validator is tie to. (

Expected Return Object:

    • An org.joget.apps.form.model.FormRowSet object which contains one or more org.joget.apps.form.model.FormRow object. 

Samples:

Load default grid data from another table if current record does not have any grid data.

Code Block
 

...

    • Element)
    • values - The submitted values of the element. (java.lang.String[])
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected Outcome:

    • A boolean value to indicate the validation pass or fail.

Samples:

Compare the submitted value with the value of another field.

Code Block
languagejava
import java.util.Arrays;
import org.joget.apps.app.service.AppUtil;
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.service.FormUtil;

public boolean validate(Element element, FormData formData, String[] values) {
    boolean result = true;

    //get field 1 value from form data object
    String field1Id = "field1";
    Form form = FormUtil.findRootForm(element);
    Element field1 = FormUtil.findElement(field1Id, form, formData);

    if (field1 != null) {
        //get value of field 1
        String[] compareValues = FormUtil.getElementPropertyValues(field1, formData);

        //compare the value of field 2 and field 1 are equals
        if (!Arrays.equals(values, compareValues)) {
            String id = FormUtil.getElementParameterName(element);
            formData.addFormError(id, "Value not equal!!!!");
            result = false;
        } 
    } else {
        //ignore if the field 1 not exist
    }

    return result; 
}

//call validate method with injected variable
return validate(element, formData, values);

Use as Form Multi Row Load Binder

Injected Variables:

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)
    • primaryKey - The primary key provided by the element to load data. (java.lang.String)
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected Return Object:

    • An org.joget.apps.form.model.FormRowSet object which contains one or more org.joget.apps.form.model.FormRow object. 

Samples:

Load default grid data from another table if current record does not have any grid data.

Code Block
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FormUtil;
import org.joget.plugin.base.PluginManager;
import org.joget.apps.form.model.FormLoadBinder;

public FormRowSet load(Element element, String primaryKey, FormData formData) {
    String defaultFormDefId = "default_grid_entry"; //change this to the form id used to store default grid data
    String formDefId = "grid_entry";  //change this to the form id used to store grid data
    String foreignKey = "fk"; //change this to the foreign key 
    
    FormRowSet f = new FormRowSet();

    // Reuse Multi Row Binder to load data
    PluginManager pluginManager = (PluginManager) FormUtil.getApplicationContext().getBean("pluginManager");
    FormLoadBinder binder = (FormLoadBinder) pluginManager.getPlugin("org.joget.plugin.enterprise.MultirowFormBinder");
    
    //Load from the grid table
    binder.setProperty("formDefId", formDefId);
    binder.setProperty("foreignKey", foreignKey);
    f = binder.load(element, primaryKey, formData);
    
    //if no grid data is retrieved, get from default table
    if (f == null || f.isEmpty()) {
        binder.setProperty("formDefId", defaultFormDefId);
        
        //set the foreign key value to empty
        f = binder.load(element, "", formData);
    }
    return f;
}

//call load method with injected variable
return load(element, primaryKey, formData);

Use as Form Multi Row Store Binder

...