Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
English
In this article, we will show how to make a field mandatory based on the value of another field.


Image Modified

Figure 1: When dropdown "Type" is set to "With Evidence", "Attachment" becomes mandatory field.

...

Place the file upload in its own section, configure the section visibility to only show such that when the dropdown value is "With Evidence".

Image Modified

Figure 2: File Upload in its own section

...

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

...