Grails-仅在更新时验证嵌入对象属性

Grails-仅在更新时验证嵌入对象属性,grails,Grails,我有一个带有嵌入对象的grails域类,我只想在更新时验证嵌入对象的属性 我知道我可以在普通的grails域类上通过使用自定义验证器并检查域的类id是否为null来实现这一点。 我不能这样做,因为嵌入式对象上缺少id 这里有一个我想做的小例子 //This is on domain/somePackage/A.groovy class A{ B embeddedObject static embedded = ['embeddedObject'] static con

我有一个带有嵌入对象的grails域类,我只想在更新时验证嵌入对象的属性

我知道我可以在普通的grails域类上通过使用自定义验证器并检查域的类id是否为null来实现这一点。 我不能这样做,因为嵌入式对象上缺少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?? 任何帮助都将不胜感激

太复杂了:

class A{
    B embeddedObject

    static embedded = ['embeddedObject']

    static constraints = {
        embeddedObject validator:{ val, obj ->
            if( obj.id && !val.attribute )
                return 'some.error.code'
        }
    }
}

是的,昨天,我试了一下,终于明白了。