Grails: Custom validator based on a previous value of the field

耗尽温柔 提交于 2019-12-01 21:18:13

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

Thx Victor Sergienko, but I do a little modification on your code

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

The difference is at this line :

Bid tempBid = Bid.get(b.id)
def oldValue = tempBid.getPersistentValue('amount')

I don't why b.getPersistentValue('amount') is always return null value, my grails version is 2.4.3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!