I a wondering is there any preffered pattern in using command objects in grails application. Particularry if i should define another method for saving objects or use the same controller method for presenting form and saving?

Let me show an example with separate methods for presenting form and saving an object

If there is a validation error the saveUser method will have to perform the whole logic that the user method did. If before displaying user form we have to load additional objects from database, perform some calculations etc... it would have to be done another time because we have to display the form again and include validation errors. Which leads to unnecessary code duplication. I cannot redirect to user method when validation fails because i would loose the command object and any errors associated with it. therefore i would not be able to display validation errors.

This example removes the need for code duplication. Saving user is only done if there was a POST request, in other case only the form is presented. In case of validation errors they are accessible and can be shown on the gsp page.

The main problem is that even when there is a GET request to the user page (which means that the user.gsp form is shown) an empty command object instance is created and validated (because it was defined in the same controller). Therefore every time the form is shown there are validation errors that provided values are empty (because command object is empty)

Both scenarios can be easily modified to work correctly (for example: saving command object before redirect in session in scenario 1 and only displaying validation errors if there was a POST request in scenario 2) but it takes additional code and does not seem to be very elegant.

The only downside is that if users submit a blank form, there will be no validation errors, but on the other hand it should be obvious that a blank form must be filled with something, why would it be there otherwise?