|
|
I am trying to create a custom validator for a variable in my domain class, such that the new value should be greater than previous.
For example, lets say the previous value was 100, next time the value should be atleast 100 or more.
Thanks in advance for the help
Code:
static constraints = { someProp validator: { val, obj -gt; val gt; obj.someProp }
}
That should do the trick.
Code:
static constraints = {
amount(validator: {val, obj -gt; val gt; obj.amount })
dateTime(nullable:false)
}
This is what I am trying to do. The custom validator does not seem to pass for any value.
Your validator is comparing the current value to itself. You need to use the getPersistentValue() method - see doc/latest/ref/Dom...tentValue.htmlCode:
static constraints = { amount validator: { val, obj -gt; def previousAmount = obj.getPersistentValue('amount') val gt; previousAmount }
}
Note that you don't need to specify quot;nullable: falsequot; since that's the default.
Originally Posted by burtbeckwithYour validator is comparing the current value to itself. You need to use the getPersistentValue() method - see doc/latest/ref/Dom...tentValue.html
You learn something new every day. Thanks Burt! |
|