Grails beforeInsert和beforeUpdate中的更改验证

Grails beforeInsert和beforeUpdate中的更改验证,grails,groovy,Grails,Groovy,我有一个域类 class Store{ String name Store hierarchy Date dateCreated Date lastUpdated static hasMany=[storeRestrictions:StoreRestrictions] boolean isBillable boolean isConsignment boolean isMassUploadPossible static c

我有一个域类

class Store{
    String name
    Store hierarchy
    Date dateCreated
    Date lastUpdated
    static hasMany=[storeRestrictions:StoreRestrictions]
    boolean isBillable
    boolean isConsignment
    boolean isMassUploadPossible

    static constraints = {
        name(nullable:false,blank:false,maxSize:50,unique:true)
        hierarchy(nullable:true,blank:true)
        dateCreated()
        lastUpdated()
        isBillable()
        isMassUploadPossible()
        storeRestrictions(nullable:false)
    }
}
例如,是否有任何方法可以更改
beforeirt
beforeUpdate
中的验证,以便如果
isConsignment
为true,那么
storeRestrictions
可以变为
null:true

我认为对您的需求来说是正确的。您可以使用检查isConsignment值的自定义验证器替换storeRestrictions上的nullable:true约束,改为执行以下操作:

static constraints = {
    ...
    storeRestrictions(validator: {field, inst -> 
                                    if( false == isConsignment) 
                                        return null != inst.storeRestrictions
                     )
}

阅读更多信息,请访问

否-不过您应该能够使用自定义验证器