JSR-303 validation with custom message - goto: answer
You can use validation groups to execute validations group-wise. For details, see section 3.4. Group and group sequence in JSR-303. In your sample you would do something like:@NotEmpty(message = "Please specify your post code")
@PostCode(message = "Your post code is incorrect", groups = Extended.class)
private String postCode;
And when validating you would call validation for the default group, then if no errors occurred, for the Extended group.
Validator validator = factory.getValidator();
Set<ConstraintViolation<MyClass>> constraintViolations = validator.validate(myClass, Default.class);
if (constraintViolations.isEmpty()) {
constraintViolations = validator.validate(myClass, Extended.class);
}
You can do much more using validation groups.
An alternative would be to make all validations (if you can afford it), and then manually filter out multiple validation errors for the same field.
Read full article from JSR-303 validation with custom message - goto: answer
No comments:
Post a Comment