GRAILS检查是否至少设置了多个属性中的一个
假设我在Grails2.x中有这样的域类GRAILS检查是否至少设置了多个属性中的一个,grails,gorm,grails-2.0,Grails,Gorm,Grails 2.0,假设我在Grails2.x中有这样的域类 class CurrentReading { DateTime timestamp Sensor sensor Integer valueInt Boolean valueBool Float valueFloat static constraints = { timestamp blank: false sensor blank: false } } 是否
class CurrentReading {
DateTime timestamp
Sensor sensor
Integer valueInt
Boolean valueBool
Float valueFloat
static constraints = {
timestamp blank: false
sensor blank: false
}
}
是否有现成的GORM/验证功能允许我确保恰好设置了一个属性valueInt
、valueFloat
或valueBool
我对grails install plugin constraints进行了一些试验,但未能将其他域属性传递给自定义验证器。您需要执行以下操作:
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()
}
}
基本上写我自己的验证器?有点难看,举个例子:)