Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using grails configuration values in domain object constraints

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?

like image 758
Raipe Avatar asked Dec 02 '25 12:12

Raipe


1 Answers

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
    }
}
like image 100
codelark Avatar answered Dec 04 '25 07:12

codelark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!