How can I use a Hibernate validator without an annotation on a field?

zinan.yumaks solution where they are creating a dummy Car object that you don't need is not the right way to do this. There is a better solution, the validateValue method:

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

String emailAddress = "...";

Set<ConstraintViolation<User>> constraintViolations = 
    validator.validateValue(User.class, "email", emailAddress);

From the documentation:

Validates all constraints placed on the property named propertyName of the class beanType would the property value be value.


Yes, it is possible. Try Something like,

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

Car car = new Car(null);

Set<ConstraintViolation<Car>> constraintViolations =
    validator.validateProperty(car, "manufacturer");

Take a look at here.

Tags:

Hibernate