Is it possible to use the Hibernate validator API storing the validated object in a field with an annotation? For example, I would like to validate Email
address by calling a Java method instead of putting an @Email
annotation on a Java bean property.
Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.
The Javax bean validation API provides the following most frequently used annotations. The Hibernate validator provides the following commonly used annotations for validation. In case of product or project development we must use both the annotations for bean validation.
Hibernate Validator Annotation Processor It helps to detect glitches such as: Putting a constraint to bean properties which are not supported by that constraint (e.g. putting @Date to a String) Annotating the setter of a bean property instead of the getter. Adding constraints to static fields or methods.
The validation class implements the ConstraintValidator interface, and must also implement the isValid method; it's in this method that we defined our validation rules.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With