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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With