Skip to content

Provide integrated support for validation within @MVC lifecycle [SPR-4975] #9650

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issuetype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions