1

我想在自定义验证器中使用标准验证器。

我想确保人口和产品字段组合只有在错误的情况下才是唯一的model_type.has_range_options。我尝试了以下方法,但它不起作用:

static constraints = {
    client validator: {val, obj, errors ->
        if (!obj.model_type?.has_range_options?.booleanValue()) {
            unique: ['population', 'product'] 
        }
    }
}

还有什么我可以尝试的吗?

4

2 回答 2

0

这是一个有趣的问题。过去,我求助于编写自己的内置约束版本,我想像你一样使用它。

我还没有弄清楚如何使用唯一约束来做到这一点,因为它作为持久约束似乎有点不同。但是,对于大多数约束,您可以像这个空白约束一样使用它们,例如:

static constraints = {
    client validator: { val, obj, errors ->
        def constraint = new org.grails.validation.BlankConstraint(propertyName: 'client', parameter: true, owningClass: this)
        constraint.validate(obj, val, errors)
    }
}
于 2015-11-09T23:40:23.553 回答
0

我刚刚写了自己独特的验证:

    static constraints = {
            client validator: {val, obj, errors ->
        if (this.findByPopulationAndClient(obj.population, obj.client) && !obj.model_type?.has_range_options?.booleanValue()) {
            errors.rejectValue('client', 'unique', "Population and Client must be unique")
        }
    }
于 2015-11-09T22:26:42.493 回答