Spring Forms - How to Check for Error on Specific Path
I'm using the Spring Form library to handle a search page in my application. Here is a snipped from my DD showing the bean configuration:
<bean name="/search.html" class="myapp.web.AccountSearchController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="searchAccounts"/>
<property name="commandClass" value="myapp.service.AccountSearch"/>
<property name="validator">
<bean class="myapp.service.AccountSearchValidator"/>
</property>
<property name="formView" value="accountSearch"/>
<property name="successView" value="accountSearch"/>
</bean>
The validator class is quite simple:
package myapp开发者_高级运维.service;
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class AccountSearchValidator implements Validator {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
public boolean supports(Class clazz) {
return AccountSearch.class.equals(clazz);
}
public void validate(Object obj, Errors errors) {
AccountSearch accountSearch = (AccountSearch) obj;
if (accountSearch == null) {
errors.rejectValue("domainName", "error.accountSearch.neither-specified", null, "Value required.");
} else if (accountSearch.getAccountId().isEmpty() &&
accountSearch.getDomainName().isEmpty() ) {
errors.rejectValue("domainName", "error.accountSearch.neither-specified", null, "Value required.");
}
}
}
In my JSP, my form is displayed in an HTML table. I want field specific errors to be displayed under the respective field as a separate table row. Here's a snippet:
<tr>
<td align="right" valign="top"><form:label path="domainName">Domain Name</form:label>:</td>
<td><form:input path="domainName" size="30"/></td>
</tr>
<tr>
<td> </td>
<td><form:errors path="domainName" cssClass="error"/></td>
</tr>
The question I have is - how can I make the output of the error row conditional on the existence of the error? Is the Validator instance accessible from my JSP? What would the test be for a c:if tag?
Thanks,
-aj
OK it took me a few days but I figured it out:
<c:set var="domainNameErrors"><form:errors path="domainName"/></c:set>
<c:if test="${not empty domainNameErrors}">
<tr>
<td> </td>
<td>${domainNameErrors}</td>
</tr>
</c:if>
This article was very helpful: http://forum.springsource.org/archive/index.php/t-51044.html
Use <spring:hasBindErrors name="loginPasswordForm">
Use <spring:bind path="fieldName">
and check status.error
to see if the field has error
Example
<spring:bind path="phoneNumber">
<form:input path="phoneNumber" />
<form:errors path="phoneNumber" cssClass="error" />
${status.error ? 'has error' : ''}
</spring:bind>
Note
<form:input path="" />
must be inside <spring:bind >
tag
Please check this code segment
<spring:bind path="domainName">
<c:if test="${status.error}">
<tr>
<td> </td>
<td><form:errors path="domainName" cssClass="error"/></td>
</tr>
</c:if>
</spring:bind>
Actually there is a simple solution. I haven't seen it anywhere but it seems to me that it is the simplest one.
<tr>
<td align="right" valign="top"><form:label path="domainName">Domain Name</form:label>:</td>
<td><form:input path="domainName" size="30"/></td>
</tr>
<form:errors path="domainName">
<tr>
<td> </td>
<td><form:errors path="domainName" cssClass="error"/></td>
</tr>
</form:errors>
When the form:errors tag has no body it renders the error with the default mecanism, but when the tag has a body, it just renders the body if there is an error for that path.
精彩评论