Provide integrated support for validation within @MVC lifecycle [SPR-4975] #9650
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: duplicate
A duplicate of another issue
type: enhancement
A general enhancement
Chris Beams opened SPR-4975 and commented
Today the
@MVC
programming model requires manual instantiation and invocation of Validator classes:public String onSubmit(
@ModelAttribute
Customer customer, BindingResult result) {new CustomerValidator().validate(customer, result);
if(result.hasErrors()) {
return "editAccount";
}
return "...";
}
It would be preferable to have validation be an integrated part of the
@MVC
lifecycle.For instance, Validator instances could be annotated as
@Component
and be subject to component scanning. At any rate, whether declared via bean definitions or component scanned, registered Validator instances should be iterated through, and any returning true from Validator#supports() should be used to validate candidate objects.Additionally, I believe the interface-based approach is preferable here, vs a
@Validator
approach, because the strong contract of supports()->validate() is a useful and expressive one. It would be sufficient to simply allow Validator instances to be component scanned. We just need to add iterating through and invoking registered validators to the lifecycle.Issue Links:
The text was updated successfully, but these errors were encountered: