Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Forms - How to Check for Error on Specific Path
    text
    copied!<p>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:</p> <pre><code>&lt;bean name="/search.html" class="myapp.web.AccountSearchController"&gt; &lt;property name="sessionForm" value="true"/&gt; &lt;property name="commandName" value="searchAccounts"/&gt; &lt;property name="commandClass" value="myapp.service.AccountSearch"/&gt; &lt;property name="validator"&gt; &lt;bean class="myapp.service.AccountSearchValidator"/&gt; &lt;/property&gt; &lt;property name="formView" value="accountSearch"/&gt; &lt;property name="successView" value="accountSearch"/&gt; &lt;/bean&gt; </code></pre> <p>The validator class is quite simple:</p> <pre><code>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() &amp;&amp; accountSearch.getDomainName().isEmpty() ) { errors.rejectValue("domainName", "error.accountSearch.neither-specified", null, "Value required."); } } } </code></pre> <p>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:</p> <pre><code> &lt;tr&gt; &lt;td align="right" valign="top"&gt;&lt;form:label path="domainName"&gt;Domain Name&lt;/form:label&gt;:&lt;/td&gt; &lt;td&gt;&lt;form:input path="domainName" size="30"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&lt;form:errors path="domainName" cssClass="error"/&gt;&lt;/td&gt; &lt;/tr&gt; </code></pre> <p>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?</p> <p>Thanks,</p> <p>-aj</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload