Is it possible somehow to create a trait with fields and constraints for those fields and then create domain classes that implement that trait and pick up the fields with constraints?
I have code that essentially looks like:
trait Shared {
String sharedField
static constraints = {
sharedField nullable: true
}
}
class ImplementingClass implements Shared {
...
}
Saving an instance of ImplementingClass with a null sharedField is then rejected with a constraint violation.
Is it possible to do this? Is there an alternative syntax that is required to use constraints and other GORM constructs in traits implemented by domain objects?
I had the same problem and I looked at the source code of Grails and made some experiments.
importFrom(Shared)
will not work because the Grails looks for a constraints
field via clazz.getDeclaredFields()
what results in an empty array for traits.
Now you have two options:
Make a Java/Groovy Class wich looks like your trait, but only contains the properties and constraints map
class SharedConstraints { String sharedField
static constraints = {
sharedField nullable: true
}
}
Now you can use SharedConstraints
with importFrom
Create a SharedConstraints.groovy in the same package:
constraints = {
sharedField nullable: true
}
If you use IntelliJ with Grails 3.0 (maybe other versions too) the script must be placed in the resources folder. If you place the file in the src/java folder as described in the Grails documentation the script gets compiled and will not work
Also note this bug https://github.com/grails/grails-core/issues/10052
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