Announcement Announcement Module
Collapse
No announcement yet.
Best practice for testing binding errors in a JSP View ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best practice for testing binding errors in a JSP View ?

    Hi everybody,

    I'm discovering (with great pleasure) both Java and SpringMVC and today I faced a problem
    that I managed to solve in two different manners, but I'm not satisfied by neither of them.
    I like to apply best practices and didn't find an answer in the documentation ..

    Consider the following html :

    Code:
        <div class="clearfix">
            <label for="identification">Identification</label>
            <div class="input">
                  <form:input class="xlarge" size="30" path="identification" />
                  <form:errors class="help-inline" path="identification" />
            </div>
        </div>
    
        <div class="clearfix">
            <label for="name">Name</label>
            <div class="input">
                  <form:input class="xlarge" size="30" path="name" />
                  <form:errors class="help-inline" path="name" />
            </div>
        </div>

    For each field, I need to append a css "error" class right after the "clearfix"
    in case of a binding/validation error for that field.

    I googled a lot and found the two following solutions :

    Code:
       <c:set var="hasError"><form:errors path="identification"/></c:set>
        <div class="clearfix <c:if test="${not empty hasError}">error</c:if>">
            <label for="identification">Identification</label>
            <div class="input">
                  <form:input class="xlarge" size="30" path="identification" />
                  <form:errors class="help-inline" path="identification" />
            </div>
        </div>
    
        <div class="clearfix <spring:hasBindErrors name="employee">
                             <c:if test="${errors.hasFieldErrors('name')}">error</c:if>
                             </spring:hasBindErrors>">
            <label for="name">Name</label>
            <div class="input">
                  <form:input class="xlarge" size="30" path="name" />
                  <form:errors class="help-inline" path="name" />
            </div>
        </div>
    It works, but it seems ugly to me and I'd like to know what is the proper way to
    address that problem. How would you deal with it ?

    Thanks in advance for your time,
    Alexandre
Working...
X