Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC jQuery remote validation
    primarykey
    data
    text
    <p>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 in the database. For the javascript I have the following:</p> <pre><code>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." } } }); </code></pre> <p>Then, I use Spring-Json to make this validation and give a response:</p> <pre><code>@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); } </code></pre> <p>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:</p> <pre><code>{result:"TRUE"} </code></pre> <p>when in fact, the answer should be:</p> <pre><code>{"TRUE"} </code></pre> <p>I don't know how to return a single response like this one using a ModelAndView answer.</p> <p>Another thing that is not working is the customized message for the "remote" validation:</p> <pre><code> messages: { title: { required: "A title is required.", remote: "This title already exists." } }, </code></pre> <p>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. At least, not mixing with jQuery remote valdations and Spring-Json. I would appreciate some help here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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