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 domain object. In this tutorial I will show you some examples of using it.

First thing you‘ll need to do is describe your command object. It is fine to do in the same file that contains controller that will use it. If command will be used by more than one controller, describe it in controllers or in 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
    }
}Code language: JavaScript (javascript)

Next thing you’ll probably want to do, is bind the data, that is being received by 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
   }  
}Code language: JavaScript (javascript)

To view errors that may appear during command validation, specify 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>Code language: HTML, XML (xml)

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 command, but also what are these errors, reject error in case if they may appear during action, check field for the error, 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()Code language: JavaScript (javascript)
Software Developer