Hey People of Groovy,
I am looking for a way to modify/change an existing closure. However, I do not wish to overwrite it; instead I would like to enhance it.
Here is a quick example. Let's say we have an Address object:
class Address {
String street
String city
String state
String zipCode
static constraints = {
street( nullable:true )
city( blank:false )
state( size:2..2 )
}
}
It would be a good example, because it is also a valid Grails domain object. What I am looking to do is to add another constraint at runtime / dynamically to the Address class:
class Address {
String street
String city
String state
String zipCode
static constraints = {
street( nullable: true )
city( blank: false )
state( size: 2..2 )
zipCode( size: 5..6 )
}
}
Notice that new zipCode
constraint?
I understand that I can change the constraints all together by overriding it through the metaClass
; however, the goal here is not to hurt anybody in a process, hence I just want to add to the existing closure.
Thanks for your ideas
/litius