I have the following domain entities:
class Customer {
// Customer properties...
static belongsTo [user: User]
}
class User {
// User properties...
}
When i try to validate a Customer instance that includes a User instance with errors, validation succeeds. In my unit test:
Customer customer = new Customer()
// Set customer properties...
User user = new User()
// Set user properties that contain errors...
customer.user = user
assertFalse user.validate() // succeeds
assertFalse customer.validate() // fails!
The same behaviour is observed on the runtime application as well. I have run my application in debug mode and verified that user’s errors property is null.
I have also tried invoking validate(deepValidate:true) on my customer instance (which according to the documentation is the default setting anyway), with no success.
Any tips?
I still have no idea why grails will not cascade validations to nested objects by default, or at least why it doesn’t work in my example.
However, i found the following workaround which works for me. In the parent class i define the following validator:
When validate() is invoked on a Customer instance, the associated User is also validated.