Reputation: 41
I wrote a very simple test case and found that Grails does only a shallow validation when i call validate on a domain object. Is it possible for me to do a deep validation in grails? Can anybody help me?
class Person {
Address address
}
class Address {
String city
}
When i do new Address().validate()
it returns false but when i do new Person(address: new Address()).validate
it returns true.
Upvotes: 4
Views: 3827
Reputation: 2193
While "deep validation" currently isn't documented for the validate()
and save()
methods, it will be in future (the document states that the documentation has been missing, while being relevant for the complete 1.3.x tree). The documentation on these methods' deepValidate
parameter then will state:
@deepValidate@ (optional) - Determines whether associations of the domain instance should also be validated, i.e. whether validation cascades or not. This is @true@ by default - set to @false@ to disable cascading validation.
Tests, however, show that "deep validation" is not performed in any of these cases:
addTo*(..)
method, e.g., person.addToAddresses(..)
validate()
and save()
methods,
deepValidate: true
parameterSimilar findings have been published at another place, categorizing the "non-behavior" as a "known issue". My own, comprehensive, test cases can be downloaded from here.
The solution, finally, is to manually invoke validation on the child object:
class Person {
Address primaryAddress
static hasMany = [secondaryAddresses: Address]
static constraints = {
primaryAddress validator: {
it?.validate()
}
secondaryAddresses validator: {
it?.every { it?.validate() }
}
}
}
Upvotes: 7