5
votes

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?

2
This is really an intuitive question which made me think if I can achieve what you are expecting using the property importFrom User in constraints for UserCommand.But no it cannot achieve the goal. Finally,I think you have to have both the messages available. Refer Validation and Internationalization where it particularly says about Class name.Unless there is another way,I feel you have to stick to above in order to apply validation in Command Object and in Domain Object.Inheritance may be an option where I am skeptical. - dmahapatro

2 Answers

5
votes

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

2
votes
  1. using unique constraint in CommandObject makes no sense, because uniqueness of what would it check?
  2. you can validate domain objects without persisting them exactly the same way as command objects - using validate() method
  3. you can put a User object in command object, set constraints only for the domain class, and then validate User object being a part of command object

    class User { 
        String name
        static constraints = {
            name blank: false, unique: true, minSize: 3
        }
    }
    
    class UserCommand {
        User user 
        static constraints = {
            user validator: { it.validate() }
        }
    }
    
    user.username.minSize.error=Please enter at least three characters.