【发布时间】:2014-09-25 17:35:21
【问题描述】:
我有一个带有嵌入对象的 grails 域类,我只想在更新时验证嵌入对象的属性。
我知道我可以在普通的 grails 域类上执行此操作,方法是使用自定义验证器并检查域的类 ID 是否不为空。 由于嵌入对象上缺少 id,我无法执行此操作。
有一个我想做的小例子。
//This is on domain/somePackage/A.groovy
class A{
B embeddedObject
static embedded = ['embeddedObject']
static constraints = {
embeddedObject.attribute validator:{val, obj-> //The app fails to start when this code is added!!
if(obj.id && !val) //if the id is not null it means the object it's updating...
return 'some.error.code'
}
}
}
//this class is on src/groovy/somePackage/B.groovy
class B{
String attribute
static constraints={
attribute validator:{val,obj->
if(obj.id && !val) //This will fail too! because the lack of an id on this object....
return 'some.error.code'
}
}
}
有没有办法在嵌入对象上获取“父级”的 id? 任何帮助将不胜感激
【问题讨论】:
标签: grails