4

How can I cancel a form submission using GWT?

I need to validate the form and process some of the data before actually submitting it.

So far I did the following, the problem is that the form is submitted even if the SubmitEvent is cancelled.

form.addSubmitHandler( new SubmitHandler() {
        @Override
        public void onSubmit(SubmitEvent event) {
            if(validate()) {
                // i do some processing here
                form.submit();
            }

            else event.cancel(); // submits anyway
        }
    });

Is this a GWT issue? How should I do?

edit: I just found out that if it was a Button that uses form.submit() on click, the submit event is cancelled. However if a SubmitButton is clicked, the event is not cancelled.

So I guess this is a browser behavior. Still, I need to find way...

edit: just found out that it does only happen in development mode, I feel silly not to have tried outside development mode before.

  • Why are you using `form.submit()` inside a `SubmitHandler`? It will get submitted, unless you explicitly `cancel()` it. – Igor Klimer Jun 28 '10 at 11:11
  • I missed that, thank you. cancel() still does not work. – Florian d'Erfurth Jun 28 '10 at 11:30
  • Hmm, I'm out of ideas - your code does exactly what the javadocs for FormPanel suggest (http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/user/client/ui/FormPanel.html). I searched GWT's issue list and there are some issues that look like what you are facing, but they've been fixed since ~1.7 - what version are you using? But I'd suggest just sticking to a normal `Button` and adding a `ClickHandler` explicitly - less hassle :P – Igor Klimer Jun 28 '10 at 12:35
  • GWT 2.0.3. I though about the button, but what about the user hitting Enter? – Florian d'Erfurth Jun 28 '10 at 13:04

1 Answers1

3

It's an issue that only happen in development mode

I have reported the issue: http://code.google.com/p/google-web-toolkit/issues/detail?id=5067