Spring 4.3.2
I need to call SmartValidator.validate() manually and I need it utilize the validation groups that I have defined on the target entity. The javadoc says this...
"This variant of validate() supports validation hints, such as validation groups against a JSR-303 provider (in which case, the provided hint objects need to be annotation arguments of type Class)."
void validate(Object target,
Errors errors,
Object... validationHints)
For some reason, I cannot find much information or examples on using "validationHints". So I have been trying things like the following...
validator.validate(targetEntity, errors, new Class[]{ValidationGroup1.class});
validator.validate(targetEntity, errors, ValidationGroup1.class);
So far, it just completely ignores my groupings. It always calls all validators. Any ideas?
Thanks!
===================================
Update: The javadoc also says this..
"Note: Validation hints may get ignored by the actual target Validator, in which case this method should behave just like its regular Validator.validate(Object, Errors) sibling."
This sounds like what's happening. But it doesn't give any clue as to why it might ignore it.
Validating a PathVariable Just as with @RequestParam, we can use any annotation from the javax. validation. constraints package to validate a @PathVariable. The default message can be easily overwritten by setting the message parameter in the @Size annotation.
The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.
Alright then. It seems the 'answer' is to not use Spring for this. Here is my workaround...
import javax.validation.Validator;
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation> violations = validator.validate(targetEntity, new Class[]{group1.class, group2.class});
Then I convert Set to Spring FieldErrors (since everything is already configured to run Spring). Kind of a clusterf***, but at least it's working now.
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