【问题标题】:Grails domain hasMany validationGrails 域 hasMany 验证
【发布时间】:2017-06-12 19:47:55
【问题描述】:

我有一个案例,验证是在域属性上完成的,而不是在关联的 (hasMany) 属性上完成的。

我可以添加任何配置来启用两个属性(域和 hasMany)的验证。

grails 版本:3.1.14

Example:

class Person {
      String name;
      static hasMany = [location: Location]
      static constraints = {
        name nullable: true
      }
}

class Location {
      String address
      String city
      State state
      String zip

      static constraints = {
        address nullable: true
      }
}

【问题讨论】:

  • 您使用的是什么版本的 Grails?将其包含在您的问题中会很有帮助。
  • grails 3.1.14 帖子也已编辑
  • schneide.wordpress.com/2010/09/20/… 你需要这样的东西

标签: grails grails-orm


【解决方案1】:

根据文档,验证应该适用于您希望的多关联:http://docs.grails.org/3.1.14/ref/Domain%20Classes/validate.html

但在我的测试中它不起作用。

另一种解决方案是使用约束:

static constraints = {
    name nullable: true
    location validator: {val, obj ->
        val.every { it.validate() } ?: 'invalid' 
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多