0

If I have an action def formPage(), I want to be able to save the form on that page, show the form with the previously entered values if there's an error, and show a blank form if it's a success.

My question is: Is it best practice for formPage to call something like saveFormPage(FormPageCommand fpc)? saveFormPage can then render formPage with or without the previously entered values. The problem with this approach is that the user can click the address bar, press enter, and then get a slew of errors since the form hasn't been filled in, and the command object sees all the null or blank values.

Another approach might be to just have a def formPage(FormPageCommand fpc), and ignore errors and just show a blank form if none of the values are filled in.

One might also call saveFormPage(FormPageCommand fpc) and redirect back to formPage() if none of the values are filled in.

What is the best practice here?

Anonymous1
  • 3,877
  • 3
  • 28
  • 42

3 Answers3

0

I'm not sure I fully understand your question, but to get you started I recommend the book "Grails in Action". It contains a lot of best practices and chapter 7 on data binding and error handling may give you some good advice and ideas.

wwwclaes
  • 1,152
  • 8
  • 12
0

Try this example:

Your controller

class MyFormController {
    def formPage() {
        [cmd: new FormPageCommand()]
    }

    def saveFormPage(FormPageCommand cmd) {
        if (cmd.hasErrors()) {
            render view: "formPage", model: [cmd: cmd]
        } else {
            // Process form
            // ......

            // Redirect back formPage() to display a new form
            redirect action: "formPage"
        }
    }
}

Your command object

class FormPageCommand {
    String optionalValue
    String requiredValue

    static constraints = {
        optionalValue(nullable: true, blank: true)
        requiredValue(nullable: false, blank: false)
    }
}

Your formPage.gsp

<g:form controller="myForm" action="saveFormPage" method="POST">

  <label>Required value *</label>
  <g:textField name="requiredValue" value="${cmd.requiredValue}"/>
  <g:if test="${cmd.errors.getFieldError('requiredValue')}">
    <g:message error="${cmd.errors.getFieldError('requiredValue')}"/>
  </g:if>

  <label>Optional value</label>
  <g:textField name="optionalValue" value="${cmd.optionalValue}"/>

  <g:submitButton name="submit"/>

</g:form>

It should serve your need.

Cheers.

David Trang
  • 1,434
  • 1
  • 10
  • 10
0

I encountered this problem before and solved it this way:

My registration form has a button:

<g:submitButton name="register" value="Register" />

So the incoming params map should have the "register" key with "Register" value.

Then I check the existence of this parameter in my controller:

def register(UserRegisterCommand urc) {
    if (params.register) {
        //some registration actions
    } 
}

In other words I check in the controller if the "Register" button was clicked, if yes - then perform registration actions. If no - the "register.gsp" is rendered.

I hope this will help.

Anton Hlinisty
  • 1,441
  • 1
  • 20
  • 35