I created some class-level validations that are working, however the property path returned when those validations are triggered is always the same. Here is my validator:
Code:
public class PurposeCodeValidator implements ConstraintValidator<PurposeCodeNotNull, Request> {
@Override
public void initialize(PurposeCodeNotNull constraintAnnotation) {
}
@Override
public boolean isValid(Request object, ConstraintValidatorContext constraintContext) {
if (object == null) {
return false;
}
String formType = object.getRequests().getFormType();
String requestType = object.getNewPrdAction();
String purposeCode = object.getPurposeCode();
if (StringUtils.contains("SF.LF.CF.CM.PEF.LDF", formType) && ! StringUtils.equals(requestType, "50") &&
StringUtils.isBlank(purposeCode)) {
return false;
}
return true;
}
}
I've added the @PurposeCodeNotNull annotation to the Request class. There are three Request objects in my object graph contained within an ArrayList. Each of the Request objects has a null purposeCode property based on the logic in my validator class and I see the following constraints when I do a print out:
requests.request[2]: Purpose Code is a required field.
requests.request[2]: Purpose Code is a required field.
requests.request[2]: Purpose Code is a required field.
If I remove the @PurposeCodeNotNull annotation from the Request class and add the @NotEmpty annotation to the purposeCode property within the Request class, the property path is printed correctly:
requests.request[0].purposeCode: may not be empty
requests.request[1].purposeCode: may not be empty
requests.request[2].purposeCode: may not be empty
Am I doing something wrong or missing something in my custom validator class? I am hoping to see this print out when using the class level validations:
requests.request[0]: Purpose Code is a required field.
requests.request[1]: Purpose Code is a required field.
requests.request[2]: Purpose Code is a required field.