I have a Car class and I want it to be validated by two different custom validators in order. I am setting the first validator on top of the class and the other one from validation-constraints-car.xml file.
@Validator1
public class Car {
private static final long serialVersionUID = 5535968331666441498L;
...
}
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
<bean class="com.galery.Car" ignore-annotations="false">
<class ignore-annotations="false">
<constraint annotation="com.galery.validation.specific.Validator2"></constraint>
</class>
</bean>
</constraint-mappings>
When the first validator fails, I don't want to execute the second validator. Right now, even if the first one fails it executes the second one and returns the messages for both of the validators. Here is my annotation interfaces and the controller method.
@RequestMapping(value = "....", method = RequestMethod.POST)
public void validateCar(@Valid @RequestBody Car car) {
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(
validatedBy = {Validator1Impl.class}
)
public @interface Validator1{
String message() default "{validator1.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@Target({ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(
validatedBy = {Validator2Impl.class}
)
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public @interface Validator2{
String message() default "{validator1.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
How can I achieve what I want? Is there any way that I can lookup the message of previous validator in ConstraintValidatorContext?
@Override
public boolean isValid(Car value, ConstraintValidatorContext context) {
...
}