Grails technologies

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 action in your controller to the command object and validate it.

1
2
3
4
5
6
7
8
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 following code in your .gsp page.

1
2
3
4
<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 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//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()

Looking to hire a software developer?
Don't hesitate to contact us.

Comments