4

I would like to check to make sure two fields are not equal and one is greater then the other. Say yearBorn and yearMarried. They cannot be equal and yearMarried must be greater then yearBorn.

Josh K
  • 28,364
  • 20
  • 86
  • 132

1 Answers1

10

You can use a 2-parameter custom validator that has access to both the value being validated and the entire instance:

static constraints = {
   yearMarried validator: { year, instance ->
      if (year == instance.yearBorn) {
         return 'i18n.code.for.equal.value'
      }
      if (year <= instance.yearBorn) {
         return 'i18n.code.for.born.after.married'
      }
   }
}
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156