Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've had the same problem: two forms (register and contact) on the same page with two submit buttons. Each form has its own validation by Spring.</p> <p>I've managed to do this in one Controller:</p> <pre><code>@RequestMapping(value = "/contactOrRegister.do", method = RequestMethod.GET) public void contactOrRegisterFormGet(@ModelAttribute("contactForm") ContactForm contactForm, @ModelAttribute("registerForm") RegisterForm registerForm) { //-----prepare forms for displaying here---- } @RequestMapping(value = "/contact.do", method = RequestMethod.POST) public String contactFormPost(@ModelAttribute("contactForm") ContactForm contactForm, BindingResult result, final Model model) { contactValidator.validate(contactForm, result); if (result.hasErrors()) { model.addAttribute("registerForm", new RegisterForm()); return "contactOrRegister"; } //--- validation is passed, do submit for contact form here --- } @RequestMapping(value = "/register.do", method = RequestMethod.POST) public String registerFormPost(@ModelAttribute("registerForm") RegisterForm registerForm, BindingResult result, final Model model) { registerValidator.validate(registerForm, result); if (result.hasErrors()) { model.addAttribute("contactForm", new ContactForm()); return "contactOrRegister"; } //--- validation is passed, do submit for register form here --- } </code></pre> <p>I need to create a new form (contact or register) and put it in model when validation is failed because "contactOrRegister" view needs both forms to display. So when "contact" form was submitted and has errors the "register" form content will be erased. This way fits for me.</p> <p>The contactOrRegister.jsp contains both forms with different action:</p> <pre><code> &lt;form:form action="/register.do" modelAttribute="registerForm" method="POST"&gt; &lt;!-- register form here --&gt; &lt;/form:form&gt; &lt;form:form action="/contact.do" modelAttribute="contactForm" method="POST"&gt; &lt;!-- contact form here --&gt; &lt;/form:form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload