Announcement Announcement Module
Collapse
No announcement yet.
@Valid ignored in Manual Controllers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Valid ignored in Manual Controllers

    Hello,

    I've created an entity (AppUser) and proper controller/views via controller scaffold.
    The entity has a field with @Size(min = 4, max = 20) validation constraint.
    With the automatic controller I'm not able to perform create operation when the field has invalid size. Fine.

    Then I wanted to do some customizations:
    I've created a manual controller:
    Code:
    controller class --class ~.controller.MyUserController --preferredMapping /appuser_my
    I've copied views/appuser/create.jspx to views/appuser_my/create.jspx.
    The manual controller has the following code:
    Code:
    @RequestMapping("/appuser_my/**")
    @Controller
    public class MyUserController {
    
        @RequestMapping(method = RequestMethod.POST)
        public String post(@Valid AppUser appUser, BindingResult result, ModelMap modelMap) {
        	System.out.println("post " + this.getClass().getName());
        	System.out.println(appUser.toString());
        	return "redirect:/appuser";
        }
        
        @RequestMapping(value = "/appuser_my/form", method = RequestMethod.GET)    
        public String createForm(ModelMap modelMap) { 
        	System.out.println("createForm");
            modelMap.addAttribute("appUser", new AppUser());        
            return "appuser_my/create";        
        }       
    }
    It turns that if I submit an invalid field value to my manual controller, the validation does not take place, and I'm able to perform the post operation with invalid values.

    I have also noticed that if I remove the BindingResult arg from my post method a validation happens and I receive an internal error screen:

    org.springframework.validation.BeanPropertyBinding Result: 1 errors Field error in object 'appUser' on field 'password': rejected value [b]; codes [Size.appUser.password,Size.password,Size.java.lang .String,Size]; arguments [org.springframework.context.support.DefaultMessage SourceResolvable: codes [appUser.password,password]; arguments []; default message [password],{javax.validation.constraints.Size.message},4,[Ljava.lang.Class;@c9537b,20,[Ljava.lang.Class;@187d27e]; default message [size must be between 4 and 20]

    I'm using Roo 1.0.0

    Why can't I produce validation behavior with my manual controller ?

    Regards,
    Renato

  • #2
    Hi,

    I've investigated the BindingResult, in my manual controller and it contains the validation errors.
    So my statement "@Valid ignored in Manual Commands" is incorrect.

    I'm able to get back to the custom jspx form if I add the following logic:
    Code:
        @RequestMapping(method = RequestMethod.POST)
        public String post(@Valid AppUser appUser, BindingResult result, ModelMap modelMap) {
        	if (result.hasErrors()) {
        		System.out.println("erros has been found: " + result.getErrorCount());
        		return "appuser_my/create";
        	} else {
    	    	System.out.println("post " + this.getClass().getName());
    	    	System.out.println(appUser.toString());
    	    	return "redirect:/appuser";
        	}
        }
    However I wonder what configuration is missing in other to get the jspx form back automatically when the validation fails - like it happens in the automatic controller/view generation.

    Comment


    • #3
      Hi Renato,

      You would need to do a conditional redirect just like it is done in the scaffolded controllers:

      Code:
      if (result.hasErrors()) {       
            [...]        
            return "owner/create";            
      }        
      return "redirect:/owner/" + owner.getId();
      Mind you, you can always take a look into the roo generated aspects to see how things are handled there. This allows you to learn how Spring / JSR 303 interaction works.

      HTH,
      Stefan

      Comment


      • #4
        Stefan

        I totally missed the conditional redirect that is built in scaffolded controllers, and started to figure out that there should be an extra configuration to do.

        My JSF background made me instinctively think about automatic redirection to the form view upon validation failure. I wasn't hoping to reach the controller method with an invalid entity. But this Spring MVC.

        Indeed the hability to inspect the entity inside the controller allows us to perform complex validations that cannot be expressed with javax.validation annotations. In my opinion, it pays off the cost of conditional redirect implementation.

        Thank you.

        Comment


        • #5
          Glad I could help .

          Comment

          Working...
          X