0

我有一个域类

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)
    }
}

有什么方法可以更改 and 中的验证beforeInsertbeforeUpdate例如,如果isConsignment为 true 那么storeRestrictions可以变为nullable:true

4

2 回答 2

0

改为这样做:

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

在Grails 验证器上阅读更多信息

于 2015-02-17T15:55:49.517 回答
0

我认为自定义验证器适合您的需求。您可以使用检查 isConsignment 值的自定义验证器替换 storeRestrictions 上的 nullable:true 约束

于 2012-06-19T10:51:41.627 回答