Results 1 to 4 of 4

Thread: How to display all form validation errors in one iteration?

  1. #1
    Join Date
    Feb 2011
    Posts
    12

    Default How to display all form validation errors in one iteration?

    I know how to use Spring's form tags and Spring MVC validation to display errors. For example, in a user registration form:

    UserName: <form:input path="userName" />
    <form:errors path="userName" cssClass="error" />

    What I don't know is how to display ALL error messages together in an iteration. I hope to display all validation errors at the top of the page. The above example displays only ONE validation error for the related field.

    Any info or example is really appreciated.

    Regards.

  2. #2
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    Code:
    <form:form>
        <form:errors path="*" cssClass="error" />
        
        ... the rest of your form fields here
    </form:form>
    In fact, <form:errors/> should do it. If you don't specify the property path, the list of all binding errors fro the form bean should be rendered by the tag.

    P.S. The Spring Reference Guide has detailed info on the Spring form tags, as well as some other quite useful info.

  3. #3
    Join Date
    Feb 2011
    Posts
    12

    Default

    constv, thanks very much for your input. that addresses my need. I should have looked at the reference guide.

    I got another need. Instead of showing all errors in one location, I want to test whether there is any form validation errors and gives the user alert message. After testing and research, I find this is a working solution:

    <c:set var="validationErrors"><form:errors path="*"/></c:set>
    <c:if test="${not empty validationErrors}">
    There are errors in your input!
    </c:if>

    However, I dont like the above <c:set>. Is there a way for me just do the following?

    <c:if test="${not empty SPRING_STUFF_GOES_HERE}">
    There are errors in your input!
    </c:if>

    It would be even better if I can test the number of errors on my form.

    Best and regards.
    Last edited by Springsky; Apr 9th, 2011 at 10:18 AM.

  4. #4
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    You can certainly do various things of this sort... In any case, it would take more than a single line. Therefore, if you need to render something other than just the error messages rendered by the <form:errors> tag, add some custom markup, info, etc, I would recommend extracting your conditional error-rendering logic into a JSP tag file (if you are using JSPs.) (I always recommend using tag files for JSP development. It is a very useful and powerful feature in JSP 2.x that allows to elegantly reuse layout markup and JSP code.) For example, you could create a tag file like this:

    Code:
    <%@tag description=
       "Displays the tag body if validation/binding errors are associated with 
        the property identified by the given path on the model attribute." %>
    <%@attribute name="path" required="true"
                 description="Path to the property on the model bean for which to check for errors." %>
    
    
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <c:if test="${not empty path}">
        <spring:bind path="${path}">
            <c:if test="${not empty status.errorMessages}">
                <%-- the content (tag body) provided by caller will be rendered here --%>
                <jsp:doBody/>
            </c:if>
        </spring:bind>
    
    </c:if>
    Note that per the JSP spec, tag files must reside under /WEB-INF/tags folder, which is the root for all other (optional) tag sub-folders. So, you could place this file in /WEB-INF/tags/core, and then use it in your pages like this:


    Code:
    ...
    <%-- your custom core tag file library definition--%>
    <%@ taglib prefix="my-core" tagdir="/WEB-INF/tags/core" %>
    ...
    <form:form ...>
    
           <my-core:if-field-errors path="yourFieldPath">
                ...whatever you want to display if field errors are detected
                (if you need to output something other than std form:errors output!)
           </my-core:if-field-errors>
     
    ...
    </form:form>
    This is just one example. You see that the body may be provided for the if-field-errors tag in this example so that each usage of the tag can potentially render different markup in each case. If all you need, for instance, is to display a red asterisk next to the invalid field, you could just create a similar no-body tag. Note the status variable exposed by the <spring:bind> tag. From that variable you can get various useful information including the number of errors, e.g. ${status.errors.errorCount}, etc.

    I can't recall or check right now whether the <spring:bind> tag accepts a wild-card for the path attribute similar to <form:errors> to be applied to all errors vs. a single field. Don't see why it shouldn't. Will leave it up to you to investigate. Good luck!
    Last edited by constv; Apr 11th, 2011 at 07:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •