In a domain class I have a value from an enum which represents an email address or a URL
class Contact{
ContactType contact
String value
}
enum ContactType{
EMAIL,
URL
}
I'm trying to use the built in grails EmailConstraint and UrlConstraint classes to validate:
static constraints = {
value(validator: {
ContactType.validate(obj, val, errors)
})
}
I managed to make it work by making some assumptions reading grails source:
Constraint constraint = new EmailConstraint()
constraint.setPropertyName "value"
constraint.setOwningClass obj.class
constraint.setParameter true
constraint.validate(obj, obj.value, errors)
But I find this very ugly, so I was wondering if anyone can offer a better solution.
Thanks.