Reputation: 1774
I'm a Grails beginner. I have a Domain class which has domainName field in Grails (ver. 2.0.1). In my DomainConstraints.groovy, I have:
constraints = {
domainName blank: false, matches: "^([^.]{0,63}\\.)*([^.]{1,63})\$"
}
but depending on some situation, i want to make this domainName field "blank: true" or "blank: false". I'm not validating my domain class against the actual database, so sync with the domain class and the actual table is not an issue.
So, I want to do something like this (code below is from my imagination):
if(something){
Domain.constraints.removeAttr('blank')
} else {
Domain.constraints.addAttr('blank', 'true')
}
Is this possible?
Upvotes: 1
Views: 1463
Reputation: 3560
You could do something like this:
class DomainConstraints {
Boolean validateBlankFlag
String domainName
static transients = [validateBlankFlag]
static constraints = {
domainName validator : { val,obj -> !val?.equals("") || !obj.validateBlankFlag}
}
}
Then just set the validateBlankFlag
on the domain object depending on whether you want to allow blank values or not. You may not even need the validateBlankFlag
property if you can make the decision based on other property values within the DomainConstraints object.
Upvotes: 2