Reputation: 3498
The docs say that depending on version, accessing Domain.constraints or Domain.constrainedProperties should give a Map of key values.
https://grails.github.io/grails2-doc/2.5.4/ref/Domain%20Classes/constraints.html
At runtime the static constraints property is a Map such that the keys in the Map are property names and the values associated with the keys are instances of ConstrainedProperty:
However, using 2.5+, accessing the constraints property at runtime doesn't give a map, but a closure, and I can't access the ConstrainedProperty instances.
I tried using grails class utils to access the static property also
GrailsClassUtils.getStaticFieldValue(Domain,"constraints")//this is still a closure
GrailsClassUtils.getStaticFieldValue(Domain,"constrainedProperties")//null, this property doesn't exist below version 3.0
Upvotes: 0
Views: 142
Reputation: 27255
See the project at https://github.com/jeffbrown/constraintsmapdemo.
https://github.com/jeffbrown/constraintsmapdemo/blob/master/grails-app/domain/demo/Widget.groovy:
package demo
class Widget {
int width
int height
static constraints = {
width range: 1..100
height range: 1..50
}
}
The test at https://github.com/jeffbrown/constraintsmapdemo/blob/master/test/unit/demo/WidgetSpec.groovy passes:
package demo
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(Widget)
class WidgetSpec extends Specification {
void "test accessing the constraints property"() {
when:
def propValue = Widget.constraints
then:
propValue instanceof Map
propValue.containsKey 'width'
propValue.containsKey 'height'
}
}
If you are not using static compilation, Widget.constraints
will evaluate to the Map
. If you are using static compilation, Widget.getConstraints()
will return the Map
but Widget.constraints
will evaluate to the closure.
Upvotes: 0
Reputation: 3498
Property access doesn't work for me like the example in the docs
Domain.constraints //returns closure
but using the method getter does
Domain.getConstraints() //returns the map
Upvotes: 2