Spring MVC jQuery remote validation
- by raulsan
Hi,
I am using Spring MVC on the server side, but in one of the pages I decided to create an AJAX validation with jQuery rather than the default Spring validation.
Everything works great, except when I have to do a remote validation to check if a "title" already exists.
For the javascript I have the following:
var validator = $("form").validate({
rules: {
title: {
minlength: 6,
required: true,
remote: {
url: location.href.substring(0,location.href.lastIndexOf('/'))+"/checkLocalArticleTitle.do",
type: "GET"
}
},
html: {
minlength: 50,
required: true
}
},
messages: {
title: {
required: "A title is required.",
remote: "This title already exists."
}
}
});
Then, I use Spring-Json to make this validation and give a response:
@RequestMapping("/checkLocalArticleTitle.do")
public ModelAndView checkLocalArticleTitle(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Map model = new HashMap();
String result = "FALSE";
try{
String title = request.getParameter("title");
if(!EJBRequester.articleExists(title)){
result = "TRUE";
}
}catch(Exception e){
System.err.println("Exception: " + e.getMessage());
}
model.put("result",result);
return new ModelAndView("jsonView", model);
}
However, this does not work and the field "title" is never validated.
I think the reason for this is that I am returning an answer in the manner:
{result:"TRUE"}
when in fact, the answer should be:
{"TRUE"}
I don't know how to return a single response like this one using a ModelAndView answer.
Another thing that is not working is the customized message for the "remote" validation:
messages: {
title: {
required: "A title is required.",
remote: "This title already exists."
}
},
The required message works, but not the remote message.
I looked around, but I didn't see many people using Spring and jQuery at the same time.
I would appreciate some help here.