Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can @Valid annotation check on fields recursively in spring mvc?

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!!!!

like image 799
Judking Avatar asked Feb 16 '13 10:02

Judking


1 Answers

I have never done recursive validation, but according to this its possible simply by tagging the sub-objects with @Valid.

like image 114
CodeChimp Avatar answered Oct 15 '22 23:10

CodeChimp