Announcement Announcement Module
Collapse
No announcement yet.
Forward form object to validator after get request Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Forward form object to validator after get request

    Hi,

    Is there a way to perform validation within a method AFTER a form backing object has been created and return any errors to the view? I am creating a form backing object using URL request parameters and I then need to perform validation on . I do have a validator set up for my Student object and it works when directly calling the validate method but I'm struggling with how to create the Student object and then manually validate it and return it. It seems like I need to be able to create the form and then forward it and form backing object to my validate method. The reason I need to do this is that the student data is provided by a feed and can be invalid, so when a user clicks on a student link I need to perform my validation on the student data and display appropriate error messages.

    For example:
    Code:
    @RequestMapping(value = "/students/updatestudent.html", method = RequestMethod.GET)
        public String updateFromStudentList(@RequestParam("id") String id) {
            
             StudentForm form =  studentService.findStudent(id);
    
            //Somehow perform validation and return command object with errors
             map.put("command", form);
             return "/students/updatestudent";
        }
    
        @RequestMapping(value = "/students/updatestudent.html", params = "validate", method = RequestMethod.POST)
        public String validateFromUpdatePage(ModelMap map, @Valid @ModelAttribute("command") StudentForm command,Errors errors) {
                  map.put("command", command);
                 return "students/updatestudent";
         }
    Thanks for any help
    Last edited by euan green; May 9th, 2012, 06:39 AM.

  • #2
    Hi,

    I've got a similar problem. I need to get an abject out of the db depending on what the request parameters are (a simple id is passed). I then need to validate the object and return a view with errors if there are any validation errors. For the life of me I can't figure out how to do this. It seems a like it should be easy to post an object to another controller method but cannot find anything that allows this.

    Hunty

    Comment


    • #3
      Hi,

      I eventually figured out a way to do it, I was actually about 90% of the way there already.
      Code:
      @RequestMapping(value = "/{id}", method = RequestMethod.GET)
      public String validateAndShowRecord(@PathVariable String id, ModelMap map)
      
      //Get form object using request param from querystring
      BackingForm form = formService.findForm(id);
      
      //Validate the form, passing in new errors object
      BindingResult errors = new BeanPropertyBindingResult(form, "command");
      validationService.checkErrors(form, errors);
      
      //Add errors and form objects to map
      map.put(BindingResult.MODEL_KEY_PREFIX + "command", errors);
      map.put("command", form);
      return viewName
      The bit that was tripping me up were the following lines
      Code:
       
      BindingResult errors = new BeanPropertyBindingResult(form, "command");
      
      map.put(BindingResult.MODEL_KEY_PREFIX + "command", errors);
      Once I put these in I was able to add the errors object to my model and it was picked up by my view

      Euan
      Last edited by euan green; May 11th, 2012, 04:21 AM.

      Comment


      • #4
        Hi,
        when I use the method with BeanPropertyBindingResult manually created I get problems with displaying dates. They`re converted into Timestamps.
        Can anyone explain this phenomenon or give me an advise how to fix it?
        Thanks
        Matthias

        Comment

        Working...
        X