Results 1 to 4 of 4

Thread: Forward form object to validator after get request

  1. #1
    Join Date
    Aug 2009
    Posts
    6

    Default 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 at 05:39 AM.

  2. #2
    Join Date
    May 2012
    Posts
    6

    Default

    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

  3. #3
    Join Date
    Aug 2009
    Posts
    6

    Default

    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 at 03:21 AM.

  4. #4

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •