I'm using spring 4.1, hibernate validator 5.1.3 for my project. I've been trying to get the GroupSequence to work from last 2 days. I've referred the validation doc, blogs and a few questions posted on stackoverflow.
Please see the below class. When I remove the GroupSequence and groups from the annotations, all the validation messages come up together, i.e, all the checks on name and other fields are fired together. Lets say for name field - I want @NotBlank and @Size to be validated first, then the name should be matched with the Pattern and at last it should be checked for @UniqueName because of the database calls.
For that, I created the GroupSequence, as suggested in the docs and answers. But when the validations are fired, only @NotBlank and @Size are fired for name. When I remove the groups value from the remaining annotations, they start working but all the error messages are shown at once, which I don't want.
I want the annotation specified with groups First.class are fired together and before Second.class validations. I don't get why the validations specified with groups aren't getting fired.
Can someone please guide me.
@GroupSequence({MyForm.class, OrderedChecks.class})
public class MyForm {
@NotBlank
@Size(min = 2, max = 40)
@Pattern(regexp = "^[\\p{Alnum} ]+$", groups = First.class)
@UniqueName(groups = Second.class)//Custom validation
private String name;
@NotBlank
@Size(min = 2, max = 40)
private String url;
@NotBlank
@Size(max = 100)
private String imagePath;
//Custom Validation
@CheckContent(acceptedType = <someString>, allowedSize=<someval>, dimensions=<someval> groups = Second.class)
private MultipartFile image
...
}
@GroupSequence(value = {Default.class, First.class, Second.class})
public interface OrderedChecks {}
@Controller
@RequestMapping(value = "/myForm")
public class MyFormController {
@RequestMapping(method = POST)
public String completeSignUp(@Valid @ModelAttribute("myForm") final MyForm myForm,
BindingResult result, RedirectAttributes redirectAttributes, Model model) {
if(result.hasErrors()) {
model.addAttribute(companyDetailsForm);
//ERROR_VIEW="myForm";
return ERROR_VIEW;
}
// Doing something else.
return <success view>;
}
}
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.
@Validated annotation is a class-level annotation that we can use to tell Spring to validate parameters that are passed into a method of the annotated class. @Valid annotation on method parameters and fields to tell Spring that we want a method parameter or field to be validated. Hope this helps.
public interface ConstraintValidatorContext. Provides contextual data and operation when applying a given constraint validator. At least one ConstraintViolation must be defined (either the default one, of if the default ConstraintViolation is disabled, a custom one).
When Spring Boot finds an argument annotated with @Valid, it automatically bootstraps the default JSR 380 implementation — Hibernate Validator — and validates the argument. When the target argument fails to pass the validation, Spring Boot throws a MethodArgumentNotValidException exception.
Use Spring's @Validated
instead of @Valid
. This will allow you specify the group and control the sequence.
Change your controller method to:
@RequestMapping(method = POST)
public String completeSignUp(@Validated(OrderedChecks.class) @ModelAttribute("myForm") final MyForm myForm,
BindingResult result, RedirectAttributes redirectAttributes, Model model) {
...
}
Note that you don't need @GroupSequence({MyForm.class, OrderedChecks.class})
on top of MyForm
bean.
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