Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

jQuery validate method and displaying messages

  • 08-05-2013 11:29PM
    #1
    Registered Users, Registered Users 2 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