8

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.

Jim Ott
  • 625
  • 6
  • 21

1 Answers1

8

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.

Jim Ott
  • 625
  • 6
  • 21
  • Springs `SmartValidator` works for me inside a SpringBoot 2 App as expected, my guess is that it depends on which underlying `Validator` springs has configured. – Leonard Brünings Jun 04 '19 at 16:09