Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 985
Josh K Avatar asked Dec 28 '22 05:12

Josh K


1 Answers

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'
      }
   }
}
like image 57
Burt Beckwith Avatar answered Jan 10 '23 06:01

Burt Beckwith