1

There are sever questions around this topic. But I couldn't find any solution for this. When we write a custom validator to validate on multiple fields of a class constraintViolation.getPropertyPath() method will not return any value. Suppose I have written a custom class level validator something like this (https://stackoverflow.com/a/2783859/3725760) to validate on two fields like stateDate and enddate(startDate<= endDate). How can I get field names?

EDIT:

Set<ConstraintViolation<T>> constraintViolationSet = validator.validate(object);
    for (ConstraintViolation<?> constraintViolation : set) {
                stringBuilder.append(constraintViolation.getPropertyPath() + " ");
                stringBuilder.append(constraintViolation.getMessage() + ". ");
                stringBuilder.append(constraintViolation.getInvalidValue() + " ");
                stringBuilder.append("is not allowed");
            }
Pruthviraj
  • 560
  • 6
  • 23
  • please add some code snipts that you have tried.. – Vikrant Kashyap Feb 21 '19 at 07:13
  • 1
    you may go through with this link might help you out --->> https://www.baeldung.com/spring-mvc-custom-validator – Vikrant Kashyap Feb 21 '19 at 07:25
  • Thanks a lot for suggesting above link. I am able to do the validation for multiple fields. But my issue is when I try to get field names involved in violations I am getting an empty value when "constraintViolation.getPropertyPath()" method is called. Which generally return field name in case of field level custom validation – Pruthviraj Feb 21 '19 at 09:07
  • assign the variable by `setPropertyPath(args)` method (If verfication failed in custom Validator) and this will reflects if you try to get by calling `getPropertyPath()` Method – Vikrant Kashyap Feb 21 '19 at 09:40

0 Answers0