1. 验证表格某列的值必须与主表单中某个字段一致
  2. 验证表格某列的值必须一致

分步指南

问题1处理步骤:
  1. 获取主表单某个字段的值
  2. 获取表格列值
  3. 比较1与2的值是否一致
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;
//获取表单字段值
String formFiledId = "#form.form1.name#";
//需要判断的表格列名
String formDataColumn = "field1";

public boolean validate(Element element, FormData formData) {
    boolean result = true;
 
    //get field 1 value from form data object
    String field1Id = formDataColumn;
    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
        for(int i = 0;i <compareValues.length ;i++){
        	if(compareValues[i] !=  formFiledId ){
        		String id = FormUtil.getElementParameterName(element);
            	formData.addFormError(id, "Value not equal !");
            	result = false;
            	break;
        	}
        }
    } else {
        //ignore if the field 1 not exist
    }
 
    return result;
}
 
//call validate method with injected variable
return validate(element, formData);


问题2处理步骤:
  1. 获取到表格的列名
  2. 循环该列所有数据检查是否一致
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;

//需要判断的表格列名
String formDataColumn = "field1";

public boolean validate(Element element, FormData formData) {
    boolean result = true;
 
    //get field 1 value from form data object
    String field1Id = formDataColumn;
    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
		String field2 = compareValues[0];  //Set the field2 value from compareValues first element
        for(int i = 0;i <compareValues.length ;i++){
			
        	if(compareValues[i] !=  field2 ){
        		String id = FormUtil.getElementParameterName(element);
            	formData.addFormError(id, "All values of the column "+ formDataColumn + " must be the equal !");
            	result = false;
            	break;
        	}
        }
    } else {
        //ignore if the field 1 not exist
    }
 
    return result;
}
 
//call validate method with injected variable
return validate(element, formData);


相关的文章