开发者

How to refer to another property in a custom Grails validator?

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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜