I had a simple register form, and validation worked fine. Something like this:
@RequestMapping(value = "/email", method = RequestMethod.POST)
public String changeEmail(@Valid @ModelAttribute("editEmail") EditEmailForm editEmailForm, BindingResult result) {
if (result.hasErrors()) {
return "editAccount";
}
userService.changeEmail(editEmailForm);
return "redirect:/";
}
and @Valid annotation did its job and if there were any errors on my form they were displayed in a correct position on my *.vm view.
But now I would like to use AJAX to send my form, so I changed my Controller to :
@RequestMapping(value = "/email", method = RequestMethod.POST)
@ResponseBody
public String changeEmail(@Valid @ModelAttribute("editEmail") EditEmailForm editEmailForm, BindingResult result) {
if (result.hasErrors()) {
return "ERROR";
}
userService.changeEmail(editEmailForm);
return "SUCCESS";
}
Now I can display the ERROR message if validation fails, but how can I display the same messages as earlier? For example I write a wrong e-mail address, and I would like to see a message that there is a wrong e-mail address. Is it possible to achieve it ?
Thanks
Dawid