我有一个父子域结构,我想为验证器访问子域中的父域数据。例如在下面的代码示例中,child1 有一个变量“名称”,出于验证器的目的,我需要 child2 数据。
我怎样才能实现这种情况?
我有这样的域结构:
class Parent{
Child child1
Child child2
static mapping = {
child1 lazy:false
child2 lazy:false
}
}
class Child{
String name
// some other variables
static belongsTo = [parent:Parent]
static constraints = {
name(nullable:true,validator:{val, obj ->
if(obj.parent){
return true
}
return false
})
}
}
我试过
this.parent.child2
但发现父母为空。
编辑:
更改:
static belongsTo = [parent:Parent]
还添加在验证器中:
if(obj.parent){
return true
}
return false
它仍然返回错误。