2

How to clear previous validation error messages coming from Command object when a user clicks on the browser back button?

Here is the sample code:

def create = { UserInfoCommand  cmd ->
    if (cmd.validate()) {
        println("Command obect Show")
        render(view:"Show");
    } else {
      println("Command obect Show")
      render(view: "Create", model: [cmd:cmd]);
    }
}

Scenario

Step 1: Submit the form without entering any data then validation messages will display on GSP from command object.

Step 2: Submit the form with the data and user navigated to the success page.

Step 3: click on the browser back button and notice previous validation messages.

Any idea what is a workaround to clear validation messages?

Tung
  • 1,579
  • 4
  • 15
  • 32
TP_JAVA
  • 1,002
  • 5
  • 23
  • 49

4 Answers4

3

just like loteq says, im catching the beforeunload and request a url that clears a session, the same way you could execute clearErrors() in controller

    $(window).bind('beforeunload', function() {jQuery.get('${createLink(action: 'clearUploadSession')}');} );
john Smith
  • 17,409
  • 11
  • 76
  • 117
2

Your problem is on the client side, not on the server side. When you click the back button, the client does not reload the page, so anything you want to be done on the server will not be done.

There is an event that you can set on the page form, that will tell the browser to reload the form every time, even on back: window.onbeforeunload. Even placing an empty event handler there, will force the page to be reloaded:

window.onbeforeunload = function () { }

Source:

http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
0

I do not know the Grails version you are working on but in Grails 2.2 you have the clearErrors() method which you can use.

See here!

Marco
  • 15,101
  • 33
  • 107
  • 174
  • I'm using 2.2 only...but we can only call clearErrors in groovy class(command or controller) but no action is being called when I click 'Back' in browser – TP_JAVA Mar 24 '13 at 23:28
0

I think that what you are looking for is the POST Redirect GET process that's explained in the wikipedia.

Post/Redirect/Get (PRG) is a web development design pattern that prevents some duplicate form submissions, creating a more intuitive interface for user agents (users). PRG implements bookmarks and the refresh button in a predictable way that does not create duplicate form submissions.

If the user ended the flow (success message like you said) the back button should go to the beginning of the flow instead of showing the cached page. If you look, this is the default behavior of the scaffold pages (create, save, edit, update). The protected post pages like save and update will issue a redirect that will avoid this cache. Just generate a controller for a domain class and you will see what I'm talking.