Gaurav Madan
Gaurav Madan

Reputation: 11

Spring MVC Form Validation - The request sent by the client was syntactically incorrect

I am trying to add form validations to a working application. I started by adding a NotNull check to Login Form. I am using Hibernate impl of Bean Validation api.

Here's the code I have written

@Controller
@RequestMapping(value="/login")
@Scope("request")
public class LoginController {

  @Autowired
  private CommonService commonService;

  @Autowired
  private SiteUser siteUser;

  @InitBinder
  private void dateBinder(WebDataBinder binder) {
      SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
      CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
      binder.registerCustomEditor(Date.class, editor);
  }

  @ModelAttribute
  protected ModelMap setupForm(ModelMap modelMap) {
    modelMap.addAttribute("siteUser", siteUser);
    return modelMap;
  }

  @RequestMapping(value="/form", method = RequestMethod.GET)
  public ModelAndView form(ModelMap map){
    if (siteUser.getId() == null){
      map.addAttribute("command",new SiteUser());
      return new ModelAndView("login-form",map);
    }else {
      return new ModelAndView("redirect:/my-dashboard/"+siteUser.getId());
    }
  }

  @RequestMapping(value="/submit", method=RequestMethod.POST)
  public ModelAndView submit(@Valid SiteUser user, ModelMap map, BindingResult result){
    if (result.hasErrors()) {
      map.addAttribute("command", user);
      System.out.println("Login Error block");
      return new ModelAndView("login/form",map);
    }
    else {
      User loggedInUser = commonService.login(user.getEmail(), user.getPassword());
      if (loggedInUser != null) {
        siteUser.setId(loggedInUser.getId());
        siteUser.setName(loggedInUser.getName());
        System.out.println("site user attr set");
      }
      return new ModelAndView("redirect:/my-dashboard/"+loggedInUser.getId());
    }
  }
}

The Model is

@Component
@Scope("session")
public class SiteUser {
private Integer id = null;
@NotNull
private String name = null;
private String email = null;
private String password = null;
private List<String> displayPrivList = null;
private List<String> functionPrivList = null;
    // And the getters and setters
}

The JSP is

    <c:url var="loginSubmitUrl" value="/login/submit"/>
    <form:form method="POST" action="${loginSubmitUrl}">
        <form:errors path="*" />
        <div class="row">
            <div class="span4">
            </div>
            <div class="span4">
                <h3>Please Login</h3>
                <label><span style="color:red">*</span>Email</Label><form:input path="email" type="text" class="input-medium" />
                <label><span style="color:red">*</span>Password</Label><form:input path="password" type="password" class="input-medium" />
                <br/>       
                <button type="submit" class="btn btn-primary">Login</button>
                <button type="button" class="btn">Cancel</button>
            </div>
        </div>          
    </form:form>

I have added messages.properties and the annotation driven bean def in the context xml. Other answers on the subject talk about form fields not getting posted. In my case, that's the expected behavior - that if I submit a blank form, I should get an error.

Please advise what am I missing?

Upvotes: 1

Views: 5672

Answers (3)

vine
vine

Reputation: 886

I think this question had the same issue as yours

Syntactically incorrect request sent upon submitting form with invalid data in Spring MVC (which uses hibernate Validator)

which just points out

You have to modify the order of your arguments. Put the BindingResult result parameter always directly after the parameter with the @Value annotation

Upvotes: 16

Rigg802
Rigg802

Reputation: 2674

You need this: <form:errors path="email" cssClass="errors" /> Use the tag form:errors for each input with the same "path" name.

It is also possible to list all the error at the same time if you don't put a path.

Here, check an full example with sample code that you can download to learn how to do: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/

Upvotes: 1

Usha
Usha

Reputation: 1468

Can you try changing the <form:form> by including the commandName to it like this

     <form:form method="POST" action="${loginSubmitUrl}" commandName="user"> 

Upvotes: 0

Related Questions