Get field name when javax.validation.ConstraintViolationException is thrown

If you inspect the return value of getPropertyPath(), you’ll find it’a Iterable<Node> and the last element of the iterator is the field name. The following code works for me:

// I only need the first violation
ConstraintViolation<?> violation = ex.getConstraintViolations().iterator().next();
// get the last node of the violation
String field = null;
for (Node node : violation.getPropertyPath()) {
    field = node.getName();
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)