I am trying to dynamically populate the static constraint ={} at run time. Is there a way to do this. Example code:
Normal statement:
static constraint = {
lastName(blank:false, maxSize: 100)
}
What I am trying to do:
static constraint = {
call to an XMLSlurper that returns a HashMap of lastName as a key and (blank: false, maxSize: 100) as a value. // This part works.
have the HashMap executed as if it where hard coded information to validate the fields. //This part does not work.
}
I hope this explains my problem well enough.
This is possible, but not the way you are trying to do it. The constraints for a GORM class are loaded using GrailsHibernateDomainClass. When the class is loaded, the evaluateConstraints method and the static property constraints is evaluated. You can look at the evaluateConstraints method in GrailsDomainConfigurationUtil to see how they are evaluated.
If you want to add your own constraints from an alternative source, you will need to modify the domain class yourself. The best way to do it is in a plugin. Read up on the plugin documentation first, and your entry point is the doWithSpring method: