0

I can successfully validate my form and even return the errors. My problems started when my view had a few selects that needed to be populated with the data coming from the controller.

When i do model.addAttribute(...) it stops passing the errors to the view:

 public String registerSubmit(@Valid @ModelAttribute("jconcorrente") Jconcorrentes concorrente, BindingResult result, HttpServletRequest request, Model model){

         if(result.hasErrors()) {
             model.addAttribute("listJdistrito", this.jdistritoService.listJdistrito());
             model.addAttribute("listJtipocodiden", this.jtipodocidenService.listJtipodociden());
             model.addAttribute("jconcorrente", new Jconcorrentes());
                return "register";
            }

So my question is how can i pass the data without affecting the validation errors?

2
  • Try to remove model.addAttribute("jconcorrente", new Jconcorrentes()); from your if. Commented Jul 6, 2015 at 11:40
  • If i do that it passes the errors but my selects don't get populated... Commented Jul 6, 2015 at 11:43

1 Answer 1

1

My guess is that your errors aren't getting populated because you are adding new instance of your model attribute directly into the model.

Try to remove this line:

model.addAttribute("jconcorrente", new Jconcorrentes());

from your if statement and create new method like this:

@ModelAttribute("jconcorrente")
public Jconcorrentes getJconcorrentes() {
    return new Jconcorrentes();
}

Leave everything else the same and it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that solved it. But i'm guessing this will always add an ModelAttribute("jconcorrente") for each method i have defined in my controller. Right? Is it possible to do this conditionally (only in the required methods)?
No it won't add it in each method, you also have to sepcify ModelAttribute("jconcorrente") in the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.