【问题标题】:How to get value of a domain constraint in Grails?如何在 Grails 中获取域约束的值?
【发布时间】:2012-04-05 14:55:34
【问题描述】:

我有一个文本字段,我想将其长度限制在我的一个域类的 maxSize 约束中。

所以如果我有一个类 foo:

class Foo {
    String bar

    static constraints = {
        bar(maxSize: 100)
    }
}

我想为属性栏获得 100 的值。这可能吗?

【问题讨论】:

    标签: grails constraints grails-constraints


    【解决方案1】:

    你应该可以做到:

    def maxBarSize = Foo.constraints.bar.getAppliedConstraint( 'maxSize' ).maxSize
    

    【讨论】:

    【解决方案2】:

    我在 grails 3.1.8 中遇到了这个问题,它已经发生了一些变化。至少在 gsp 视图中我不得不这样说:

    Foo.constrainedProperties ['bar']['maxSize']
    

    希望对您有所帮助!干杯!

    【讨论】:

    【解决方案3】:

    检查以下代码:

    def foo = new Foo(bar: "stuff")
    println foo.constraints.bar.maxSize
    

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多