【问题标题】:Grails validation dependent on other attributes依赖于其他属性的 Grails 验证
【发布时间】:2011-08-20 11:49:10
【问题描述】:

用 grails 做这样的事情的正确方法是什么:

class myDomainThing {
  String description
  MyOtherDomainThing otherThing

  static constraints = {
    description(nullable:if(otherThing))
    otherThing(nullable:if(description))
  }
}

所以我要么想要一个指向 otherDomainThing 的链接,要么想要一个字符串描述。

【问题讨论】:

    标签: grails grails-validation


    【解决方案1】:

    您必须使用 Grails 自定义验证,使用 validator

    static constraints = {
      description(validator: {
            return otherThing and !description
        })
    }
    

    【讨论】:

      【解决方案2】:

      您需要使用自定义验证器

      static constraints = {
        description validator: { val, obj -> 
           if(otherthing && val) {
               false
           }
           else {
             true
           }
        }
      }
      

      显然otherthing 周围有一些伪代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-16
        • 1970-01-01
        • 2019-12-17
        • 2016-07-08
        • 1970-01-01
        • 2016-03-19
        • 2011-09-09
        • 1970-01-01
        相关资源
        最近更新 更多