Josh K
Josh K

Reputation: 28883

Custom Grails validation

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.

Upvotes: 4

Views: 1056

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

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'
      }
   }
}

Upvotes: 10

Related Questions