Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

jQuery validate method and displaying messages

Options
  • 08-05-2013 11:29pm
    #1
    Registered Users Posts: 40


    I am using Spring MVC, jQuery validate plugin and I am trying to display custom error messages. My code is working fine. The url i specified in remote: connects to my MVC controller. The logic in the controller executes fine. The jQuery files are added correctly too.

    My problem is how do I get the error message in messages: to display in my JSP file. I have added my code. (Note: I am not sure if I understand the remote function in the jQuery file. I think you have to return true or false. I am going on the jQuery documentation.). Perhaps someone can help. Thanks.

    register.jsp
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    
    <c:if test="${not empty message}">
        <div id="message" class="${message.type}">${message.message}</div>
    </c:if>
    
    <script type="text/javascript">
    $(document).ready(function(){
        $("#reg").validate({
            rules: {
                email: {
                    required: true,
                    email: true,
                    remote: 'clientside/emailsearch'
                }      
            },
            messages:{
                email:{
                    required: "You need an email",
                    email: "This is not valid",
                    remote: "Someone has this email already!"
                    
                }
            }
        });
    });    
    </script>
    
    <form:form id="reg" method="POST" modelAttribute="farmer">
        <fieldset>
            <legend><spring:message code="label_register"/></legend>
            <table>
                <tr><td><form:label path="herdNo" cssErrorClass="error"><spring:message code="label_farmer_herdno" /></form:label></td><td><form:input id="herdNo" path="herdNo" /></td><td class="error"><form:errors path="herdNo" /></td></tr>        
                <tr><td><form:label path="name" cssErrorClass="error"><spring:message code="label_farmer_name" /></form:label></td><td><form:input id="name" path="name" /></td><td><form:errors path="name"/></td></tr>
                <tr><td><form:label path="addressLine1" cssErrorClass="error"><spring:message code="label_farmer_address1" /></form:label></td><td><form:input id="addressLine1" path="addressLine1" /></td><td><form:errors path="addressLine1"/></td></tr>
                <tr><td><form:label path="addressLine2" cssErrorClass="error"><spring:message code="label_farmer_address2" /></form:label></td><td><form:input id="addressLine2" path="addressLine2" /></td><td><form:errors path="addressLine2"/></td></tr>
                <tr><td><form:label path="addressLine3" cssErrorClass="error"><spring:message code="label_farmer_address3" /></form:label></td><td><form:input id="addressLine3" path="addressLine3" /></td><td><form:errors path="addressLine3"/></td></tr>
                <tr><td><form:label path="addressLine4" cssErrorClass="error"><spring:message code="label_farmer_address4" /></form:label></td><td><form:input id="addressLine4" path="addressLine4" /></td><td><form:errors path="addressLine4"/></td></tr>
                <tr><td><form:label path="phoneNo" cssErrorClass="error"><spring:message code="label_farmer_phoneno" /></form:label></td><td><form:input id="phoneNo" path="phoneNo" /></td><td><form:errors path="phoneNo"/></td></tr>
            </table>
        </fieldset>
        <fieldset>
            <legend><spring:message code="farmer_login_details"/></legend>
            <table>
                <tr><td><form:label path="username" cssErrorClass="error"><spring:message code="label_farmer_username" /></form:label></td><td><form:input id="username" path="username" /></td><td><form:errors path="username"/></td></tr>
                <tr><td><form:label path="password" cssErrorClass="error"><spring:message code="label_farmer_password" /></form:label></td><td><form:password id="password" path="password" /></td><td><form:errors path="password"/></td></tr>
                <tr><td><form:label path="email" cssErrorClass="error"><spring:message code="label_farmer_email" /></form:label></td><td><form:input id="email" path="email" /></td><td><form:errors path="email"/></td></tr>
            </table>
        </fieldset>
        <button id="save"><spring:message code="button_save"/></button>
    </form:form>
    
    MVC controller
    package com.jamesanthony527.herdmanager.controller;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.jamesanthony527.herdmanager.service.FarmerService;
    
    @RequestMapping("/clientside")
    @Controller
    public class ClientJsController {
        
        private static final Logger logger = LoggerFactory.getLogger(ClientJsController.class);
    
        
        @Autowired
        private FarmerService farmService;
        
        @RequestMapping(value="/emailsearch", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public @ResponseBody boolean checkMail(@RequestParam String email){ 
            
            logger.info("Well got to the method!");
            
            logger.info(email);
            
            if(farmService.findByEmail(email) != null) {
                
                logger.info("The query worked");
                return true;
            }
            else{
                return false;
            }
            
        }
    }
    


Advertisement