Hi Hardy!
Yep, @Valid is validating the collection just fine - this is a problem with the property path.
For example, if a component has @Valid and is contained within another object that is the parent, Hibernate Validator will create the proper path.
So let's say we have Customer and Address. If @Valid is on top of Address, Hibernate Validator will correctly say, "customer.address.street" <- has the error. The property path is correct.
Now, in order to satisfy spring mvc, I need to do the same thing with collections. However, Hibernate validator is not constructing the path.
Code:
class Country {
@Valid
List<Province> provinces = new ArrayList<Province>();
....
}
if there is a problem with a property on Province, even though we are starting from Country, Hibernate Validator will not make the property path:
Code:
country.provinces[3].name <- property path.
Instead, Hibernate Validator just says, "name" for the property path.
However, since Country is the parent object and there is no name property, this is incorrect.
In my actual case, this is the output form Hibernate Validator:
Code:
DEBUG ProcessForm:55 - Constraint Violated: {questionnaireAnswer.text.notEmpty}
DEBUG ProcessForm:84 - Adding error on path [text] with message [Text may not be empty]
And this is the error spring throws as a result:
Code:
* NotReadablePropertyException: Invalid property 'text' of bean class [myproject.domain.candidate.Candidate]: Bean property 'text' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
Candidate (in my application) has no "text" property. In fact, this text property is a property of an object that is inside of a collection contained within Candidate. In a sense, Candidate is Country and the questionnaireAnswer is the province.
What would be helpful is Hibernate validator giving collection-aware paths ;) Or is there a way to do it? Am I doing it wrong?
Thanks Hardy.