开发者

Custom Grails validation

开发者 https://www.devze.com 2023-01-24 18:58 出处:网络
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开发者_Go百科 equal and yearMarried must be greater then year

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开发者_Go百科 equal and yearMarried must be greater then yearBorn.


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

精彩评论

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