Exdevfr
Exdevfr

Reputation: 5

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

Upvotes: 0

Views: 794

Answers (2)

saw303
saw303

Reputation: 9072

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']

Upvotes: 1

Exdevfr
Exdevfr

Reputation: 5

That way, which was valid in Grails 2, still works...

grailsApplication.getArtefact('Domain',
    'MyDomainClass').getConstrainedProperties().myProperty.inList.collect{it.name()}

see GrailsDomainClass API

Upvotes: 0

Related Questions