Validation Grails域:如何访问父域数据?
我有一个父子域结构,我想访问验证程序子域中的父域数据。例如,在下面的代码示例中,child1有一个变量“name”,出于验证程序的目的,我需要child2数据 我怎样才能达到这种情况 我有如下域结构:Validation Grails域:如何访问父域数据?,validation,grails,groovy,gorm,domain-object,Validation,Grails,Groovy,Gorm,Domain Object,我有一个父子域结构,我想访问验证程序子域中的父域数据。例如,在下面的代码示例中,child1有一个变量“name”,出于验证程序的目的,我需要child2数据 我怎样才能达到这种情况 我有如下域结构: class Parent{ Child child1 Child child2 static mapping = { child1 lazy:false child2 lazy:false } } class Child{
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(对象父对象){
返回真值
}
返回false
它仍然返回false。替换
static belongsTo = [Parent]
与
因此,孩子知道自己的父母要基于@bassmartin的答案,请检查自定义验证器。验证器应声明(至少)两个参数,其中第二个是对象实例:
validator: { val, obj ->
//obj.parent is what you're looking for
}
obj.parent仍然为空。少了什么吗?
validator: { val, obj ->
//obj.parent is what you're looking for
}