0

假设我在 Grails 2.x 中有类似的域类

class CurrentReading {

    DateTime timestamp
    Sensor sensor
    Integer valueInt
    Boolean valueBool
    Float valueFloat

    static constraints = {
        timestamp blank: false 
        sensor blank: false
    }

}

是否有一个开箱即用的 GORM / Validation 功能,可以让我确保恰好是其中一个属性valueIntvalueFloat或者valueBool已设置?

我已经做了一些实验grails install-plugin constraints,但未能将额外的域属性传递给自定义验证器。

4

1 回答 1

0

你需要做这样的事情:

static constraints = {
    type inList:['Foo', 'Bar', 'FooBar']
    idStr blank: false, validator: { val, obj ->
        def result
        if(obj.type == "FooBar") {
            result = val =~ /[a-zA-Z]{2}[0-9]{8}/
        } else if(['Foo', 'Bar'].contains(obj.type)) {
            result = val =~ /\d+/
        }
        result.matches()
    }
}
于 2013-01-08T16:20:35.147 回答