I am attempting to validate my object that encapsulates a list of other objects, as follows (shortened for brevity):
public class FormDTO {
private List<AttributeDTO> ruleAttributes;
// More attributes here
}
public class AttributeDTO {
private String value;
// More attributes here
}
A snippet of my validator is as follows:
for(AttributeDTO attributeDTO : attributes)
{
if(attributeDTO.getValue() == null || attributeDTO.getValue().length() == 0)
{
errors.reject("value", "value.isEmpty");
}
}
My jsp contains the following:
<c:forEach items="${form.ruleAttributes}" var="ruleAttribute" varStatus="counter">
<tr>
<td>
<c:choose>
<c:when test="${ruleAttribute.isEditable}">
<form:input path="ruleAttributes[${counter.index}].value" value="${ruleAttribute.value}"/>
</c:when>
<c:otherwise>
<span class="derived">NotEditable</span>
</c:otherwise>
</c:choose>
</td>
<td>
<form:errors path="ruleAttributes[${counter.index}].value"/>
</td>
</tr>
</c:forEach>
How do I get the corresponding error message to appear for the relevant list item? In summary, I want the “value.isEmpty” message to appear in the table cell for the relevant row that has an empty value.
Thanks
After reading the Spring reference guide again, I can answer this question myself.
To get the appropriate error to appear for this snippet…
…I need to modify my validation code as follows:
}