views:

48

answers:

1

I have a property that can be nullable or required depending on the status of another variable.

class Person{
name()
civilStatus(inList:['Single','Married','Divorced','Widowed'])
partnerOrSpouse()
}

the partnerOrSpouse property is nullable or not depending on the value of the civilStatus property.

+2  A: 

You can use a custom validator. Using the two-parameter version, the first is the value being validated and the second is the domain class instance. You can refer to other properties via the 'obj' parameter:

class Person {
   ...
   static constraints = {
      name()
      civilStatus inList:['Single','Married','Divorced','Widowed']
      partnerOrSpouse validator: { val, obj ->
         if (obj.civilStatus == 'Single') {
            return 'some.error.code'
         }
      }
   }
}
Burt Beckwith
Thanks will try.
Neoryder