繁体   English   中英

在自定义验证器中使用标准验证器进行Grails验证

[英]Grails validation using standard validators in custom validator

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

我想确保填充和乘积字段组合唯一,如果model_type.has_range_options为false。 我已经尝试了以下方法,但无法正常工作:

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

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

我最后写了自己的独特验证:

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

这是一个有趣的问题。 过去,我依靠自己的方式编写自己想使用的内置约束版本。

我还无法弄清楚如何使用唯一性约束来执行此操作,因为它作为持久性约束的工作方式似乎有所不同。 但是,对于大多数约束,您可以像空白约束一样使用它们,例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM