Search code examples
grailsgrails-ormgrails-3.0

Grails 3 Accessing Grails domain constraints at runtime


I used to access grails 2 constraints in my gsp like this :

${MyDomainClass.constraints.myProperty.inList.collect{it.name()}} 

It doesn't work in Grails 3 anymore


Solution

  • In Grails 3.0 domain and command objects are using the trait grails.validation.Validateable (Source can be found here). That trait gives you access to the constraints by providing the following method.

    static Map<String, ConstrainedProperty> getConstraintsMap();
    

    In order to access the constraints you call that method on your domain or command object. The following example accesses the nullable constraint on a domain objects property called day.

    domainObject.getConstraintsMap()['day']['nullable']