Hello,
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