我有一个看起来像这样的 Grails 域对象:
class Product {
Boolean isDiscounted = false
Integer discountPercent = 0
static constraints = {
isDiscounted(nullable: false)
discountPercent(range:0..99)
}
我想添加一个验证器,discountPercent
它只会验证是否isDiscounted
为真,如下所示:
validator: { val, thisProduct ->
if (thisProduct.isDiscounted) {
// need to run the default validator here
thisProduct.discountPercent.validate() // not actual working code
} else {
thisProduct.discountPercent = null // reset discount percent
}
有谁知道我该怎么做?