Is there a good/standard way to execute some common code before every save()
invocation on domain classes?
For example, my domain
class Page {
String url
Boolean processed
Date date
Integer urlCrc
}
My form has only 3 first fields and I would like to calculate urlCrc
every time the save()
method is called. I cannot just override save method because it is injected.
You can use GORM events - see the docs. Since by default validate()
is called before every save()
I would use that.
class Page {
//your defs here
def beforeValidate() {
this.urlCrc = yourComputationHere
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With