I make a model object with some JSR-303 validator annotation:
public class UserInfoBasicModel implements Serializable{
@NotNull(message="cannot be null")
@NotEmpty(message="cannot be empty")
private String name;
//getter and setter
//..ignored..
}
Auto data-binding it in a controller:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(method = RequestMethod.POST, value = "/registry/")
public String registry(HttpServletRequest request,
ModelMap modelMap,
@Valid UserInfoBasicModel userInfoBasicModel,
BindingResult result) {
//...some code here...
}
}
In the above scenario, it works fine for the validation. But when I encapsulate the model into another object just as below, the validation on UserInfoBasicModel doesn't work anymore:
the Object that encapsulates the UserInfoBasicModel object:
public static class UserUpdateFormTransmitter {
@Valid
private UserInfoBasicModel userInfoBasicModel;
//getter and setter
//..ignored..
}
the controller:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(method = RequestMethod.POST, value = "/registry/")
public String registry(HttpServletRequest request,
ModelMap modelMap,
@Valid UserUpdateFormTransmitter userUpdateFormTransmitter,
BindingResult result) {
//...some code here...
}
}
I'm wondering why doesn't the @valid annotaion works recursively just like what JSR 303: Bean Validation says.Could any one give me a solution so that I can valid my object recursively, thanks a lot!!!!
I have never done recursive validation, but according to this its possible simply by tagging the sub-objects with @Valid.
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