代码之家  ›  专栏  ›  技术社区  ›  Trebla

Grails动态“inList”

  •  1
  • Trebla  · 技术社区  · 6 年前

    这几乎与这个老问题相同: Dynamic define the inList constraint using database query 这一问题基本上没有得到解决,也许在提出这一问题后的几年里取得了进展。

    我想填充 inList 域属性的参数,其值来自另一个域。由于自动生成的视图(脚手架、过滤板),这需要来自 inList(内部列表) 而不是自定义验证器。

    class MyDomain {
      String someValue
      static constraints = {
        someValue(nullable: true, maxSize: 50, inList: SomeOtherDomain.list()*.name)
      }
    }
    

    这会在启动时产生以下错误:

    Caused by: java.lang.IllegalStateException: Either class [thepackage.SomeOtherDomain] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.

    我知道,正确的处理方法是 someValue 的实例 SomeOtherDomain 而不仅仅是存储名称,但这不太合适。我们希望能够删除的实例 SomeOtherDomain其他域 不破坏所属域的保存值。。。具有删除值的域今后将无效,必须在保存前更新,但存档/锁定的记录仍将存在并可以显示。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Daniel    6 年前

    可以如下所示为filterpane指定值列表:

    <filterpane:filterPane domain="MyObject" filterPropertyValues="${['someValue':[values: SomeOtherDomain.list().collect{it.name}]]}" />
    

    然后使用自定义验证器进行实际验证。我不确定什么样的脚手架可能会使用inList,但如果您可以用静态页面替换一些脚手架页面,那么就可以很容易地绕过它。