【问题标题】:Grails - Validation of embedded object attributes only while updatingGrails - 仅在更新时验证嵌入对象属性
【发布时间】: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


    【解决方案1】:

    太复杂了:

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

    【讨论】:

    • 是的,昨天试了之后,我想通了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    相关资源
    最近更新 更多