I might be wrong here but if you're using just the stock Grails constraints, the only way to share a validation message is to simply rely on the default.x.x.message
key/values in messages.properties. Otherwise messages are looked up via the following key form:
className.propertyName.errorcode...=
You can however use a custom validator and override what message key gets returned for the validation error.
class User {
...
static constraints = {
...
name blank: false, unique: true, validator: { value, user ->
if(!value || value.length() < 3)
return 'what.ever.key.in.messages.properties'
}
}
}
Then you can keep it all DRY by sharing constraints between classes via a global constraint or as @dmahapatro mentioned, with the use of an importFrom
in your UserCommand like so,
class UserCommand {
...
static constraints = {
importFrom User
...
}
}
If you have more complicated validation, you can create your own constraints classes. Here are some resources:
http://www.zorched.net/2008/01/25/build-a-custom-validator-in-grails-with-a-plugin/ http://blog.swwomm.com/2011/02/custom-grails-constraints.html