Normally for a Grails domain or command class, you declare your constraints and the framework adds a validate()
method that checks whether each of these constraints is valid for the current instance e.g.
class Adult {
String name
Integer age
void preValidate() {
// Implementation omitted
}
static constraints = {
name(blank: false)
age(min: 18)
}
}
def p = new Person(name: 'bob', age: 21)
p.validate()
In my case I want to make sure that preValidate
is always executed before the class is validated. I could achieve this by adding a method
def customValidate() {
preValidate()
validate()
}
But then everyone who uses this class needs to remember to call customValidate
instead of validate
. I can't do this either
def validate() {
preValidate()
super.validate()
}
Because validate
is not a method of the parent class (it's added by metaprogramming). Is there another way to achieve my goal?