Skip to content

About @ModelAttribute, two commands, BindingResult [SPR-7358] #12016

Description

@spring-projects-issues

kendy lau opened SPR-7358 and commented

How to deal with two @ModelAttribute(s) like the follows:

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("owner") Owner owner, @ModelAttribute("contact") Contact contact,
BindingResult result, SessionStatus status) {

	new OwnerValidator().validate(owner, result);	
	if (result.hasErrors()) {	
		return "owners/form";
	}	
	else {	
		this.clinic.storeOwner(owner);
		status.setComplete();
		return "redirect:/owners/" + owner.getId();
	}	
}		

When error occures during the OwnerValidator validate the command owner, the BingingResult don't know which command object is the errors binded for.

the validator is like this

public class OwnerValidator {

public void validate(Owner owner, Errors errors) {		
	if (!StringUtils.hasLength(owner.getFirstName())) {	
		errors.rejectValue("firstName", "required", "required");
	}	
	if (!StringUtils.hasLength(owner.getLastName())) {	
		errors.rejectValue("lastName", "required", "required");
	}	

.....


Affects: 3.0.2

Attachments:

Issue Links:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions