Вопрос

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?

Это было полезно?

Решение

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

Другие советы

  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.
    
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top