Versions Compared

Key

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

...

Code Block
languagejava
titleBean Shell Validator in File Upload
linenumberstrue
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[] fileUploadValues) {
    boolean result = true;
    
    //get dropdown select box value
    String field1Id = "type";
    Form form = FormUtil.findRootForm(element);
    Element field1 = FormUtil.findElement(field1Id, form, formData);
 
    if (field1 != null) {
        //get value of field 1
        String[] field1Values = FormUtil.getElementPropertyValues(field1, formData);
 
        if(field1Values[0].equalsIgnoreCase("With Evidence") && fileUploadValues[0].isEmpty() ){
            String id = FormUtil.getElementParameterName(element);
            formData.addFormError(id, "Attachment is required when type is 'with evidence'");
            result = false;
        }
    } else {
        //ignore if the dropdown does not exist
    }
 
    return result;
}
 
//call validate method with injected variable
return validate(element, formData, values);

...