Grails Command Objects custom Validate Message Codes

confile picture confile · Jun 10, 2013 · Viewed 7.4k times · Source

When using command objects like:

class UserCommand {

   String name

   static constraints = {
      name blank: false, unique: true, minSize: 3
   }
}

you can use them to validate objects without making them persistent. In my case I would validate for a persistent class User.

In controller:

def save(UserCommand cmd) {
  if(!cmd.validate()) {
      render view: "create", model: [user: cmd]
  return
  }
  def user = new User()
  user.name = cmd.name
  user.save()

  redirect uri: '/'

} 

in messages.properties:

user.username.minSize.error=Please enter at least three characters.
userCommand.username.minSize.error=Please enter at least three characters.

When using custom validation messages you have to write the message codes for each error twice. One for the User class and another for the UserCommand class.

Is there a way how I can have only one message code for each error?

Answer

ikumen picture ikumen · Jun 11, 2013

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