How to get value of a domain constraint in Grails?
Asked Answered
H

3

11

I have a text field whose length I would like to limit at the maxSize constraint of one of my domain classes.

So if I have a class foo:

class Foo {
    String bar

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

I would like to get that value of 100 for the property bar. Is this possible?

Heliotropin answered 5/4, 2012 at 14:55 Comment(0)
D
14

You should be able to do:

def maxBarSize = Foo.constraints.bar.getAppliedConstraint( 'maxSize' ).maxSize
Doily answered 5/4, 2012 at 14:59 Comment(1)
Checkout "Programmatic access" section in docs.grails.org/latest/ref/Constraints/Usage.html for modern Grails > 3Sistrunk
C
10

I was having this issue in grails 3.1.8 and it has change a bit. at least in the gsp views I had to put this:

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

Hope this help! Cheers!

Canonist answered 8/7, 2016 at 14:9 Comment(1)
Checkout "Programmatic access" section in docs.grails.org/latest/ref/Constraints/Usage.htmlSistrunk
W
1

Check the following code:

def foo = new Foo(bar: "stuff")
println foo.constraints.bar.maxSize
Windmill answered 5/4, 2012 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.