Question

Grails 2.2.0

How do I access the custom configuration variables in a Grails domain object constraints.

I would like to have something like this:

    class User {

         def grailsApplication

         String name

         static constraints = {
             name size: grailsApplication.config.maxlength
         }  

    }

But it fails with "No such property: grailsApplication". I have tried to get it work by following suggestions in getting grails 2.0.0M1 config info in domain object, and static scope? but have not managed to get any combination to work.

How do I access config in domain object constraints? In addition how do I handle such a case in a unit test for the domain constraints?

Was it helpful?

Solution

You can use the grails.util.Holders class to get access to the configuration object as follows:

In Config.groovy:

myMaxSize = 10

In your domain class:

class User {
    String name

    static constraints = {
        name minSize: Holders.config.myMaxSize
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top