Grails:基于字段的先前值的自定义验证器 [英] Grails: Custom validator based on a previous value of the field

查看:110
本文介绍了Grails:基于字段的先前值的自定义验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在我的域类中为变量'amount'创建一个自定义验证器,以使新值大于0.50。例如,可以说前一个值是1.0,下一次该值应该是至少:[先前值+ 0.50]或更多。



预先感谢您的帮助

解决方案

您可以尝试阅读 domainEntity.getPersistentValue('amount')



编辑: ... in自定义验证器,如:

  class出价{
双数
...
static constraints = {amount(validator:{double a,Bid b - >
def oldValue = b.getPersistentValue('amount')
a> oldValue + 0.5?true:Amount $ a should be at至少$ {oldValue + 0.5}})
}
}


I am trying to create a custom validator for variable 'amount' in my domain class, such that the new value should be greater than previous by 0.50.

For example, lets say the previous value was 1.0, next time the value should be atleast: [previous value + 0.50] or more.

Thanks in advance for the help

解决方案

You can try reading domainEntity.getPersistentValue('amount').

EDIT: ... in custom validator, like:

class Bid { 
  Double amount 
  ...
  static constraints = { amount(validator: { double a, Bid b -> 
    def oldValue = b.getPersistentValue('amount')
    a > oldValue + 0.5 ? true : "Amount $a should be at least ${oldValue  + 0.5}" }) 
  }
}

这篇关于Grails:基于字段的先前值的自定义验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆