Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show binding errors next to each input

How do I show validation errors NEXT to each input/component?

Validator:

@Override
public void validate( final Object obj, final Errors e )
{
   ValidationUtils.rejectIfEmpty( e, "firstname", "error.firstname.empty" );
}

JSP:

<form:label  path="firstname">
   <spring:message code="label.firstname" />
</form:label>
<form:input  path="firstname" />
<form:errors path="firstname" /> <!-- THIS DOES NOT WORK! -->

I can show all errors by using the following view code:

<spring:hasBindErrors name="contact">
   <ul>
      <c:forEach var="error" items="${errors.allErrors}">
         <spring:message code="${error.code}"></spring:message>
      </c:forEach>
   </ul>
</spring:hasBindErrors>

Any ideas?

like image 544
eventhorizon Avatar asked Jul 27 '10 23:07

eventhorizon


1 Answers

Hopefully you've already figured it out.

You could do the following to display all errors:

<spring:bind path="contactUs.*">
  <c:if test="${status.errors.errorCount > 0}">
    <ul>
    <c:forEach var="error" items="${status.errors.allErrors}">
    <li><spring:message message="${error}"></spring:message></li>
    </c:forEach>
    </ul>
  </c:if>
</spring:bind>

If what you need is to display each form input and it's binding error next to each other you'll need to do:

<spring:bind path="contactUs.email">
  <input value="${status.value}" name="${status.expression}">
    <c:if test="${status.error}">
      Error codes:
      <c:forEach items="${status.errorMessages}" var="error">
        <c:out value="${error}"/>
      </c:forEach>
    </c:if>
  </input>
</spring:bind>
like image 108
user686519 Avatar answered Oct 14 '22 00:10

user686519