Command Object in Grails

Using command objects in Grails is a simple way to perform data binding and validation when there is no need to create a domain object. In this tutorial, I will show you some examples of using it.

The first thing you‘ll need to do is describe your command object. It is fine to do in the same file that contains the controller that will use it. If the command will be used by more than one controller, describe it in the controllers or in the Groovy source directory.

Note: all the code below is written and tested against Grails 1.3.7

class RegisterCommand {
    String login
    String email
    String password
    String confirmationPassword

    static constraints = {
        login size: 1..250, blank: false
        email size: 1..250, email: true, blank: false
        password size: 6..20, blank: false
        confirmationPassword size: 6..20, blank: false, validator: RegisterController.passwordValidator
    }

    static final def passwordValidator { String value, command ->
        if (command?.password != command?.confirmationPassword) {
            //returning message code for i18n/messages.properties file
            return 'registerCommand.confirmationPassword.mismatch'
        }

        true
    }
}

Next thing you’ll probably want to do is bind the data that is being received by the action in your controller to the command object and validate it.

def registerAction = { RegisterCommand command -> // data binding and validation
   //checking errors  
   if (command.hasErrors()) {
       render view: 'register', model: [command: command]
   } else {
       //do something
   }  
}

To view errors that may appear during command validation, specify the following code in your .gsp page.

<g:textField name="confirmationPassword" bean="${command}"/>
<g:hasErrors bean="${command}" field="confirmationPassword">
   <s2ui:fieldErrors bean='${command}' field=”confirmationPassword”/>
</g:hasErrors>

Now this is a pretty common usage of command objects, but you may need some more functionality. You can check not only whether there is any error in the command, but also what these errors are, reject errors in case they may appear during the action, check the field for errors, clear all the errors, and so on.

//checking is specified login already exists
if (Account.findByLogin(command.login)) {
   //rejects error for field in command, which validation has failed
   command.errors.rejectValue('login', 'registerCommand.login.exists')    
}

//checks is specified field contains invalid value
if (command.errors.hasFieldErrors('email')) {
   //do something
}
//shows errors
command.getErrors()
//clears errors
command.clearErrors()
Software Developer