开发者

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

开发者 https://www.devze.com 2022-12-30 16:48 出处:网络
I have a property that can be nullable or required depending on the status of another variable. class Person{

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

精彩评论

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