FormPanel.SubmitEvent cancel not working

365 views
Skip to first unread message

Greg Dougherty

unread,
Jan 5, 2011, 3:27:47 PM1/5/11
to Google Web Toolkit
I have the following code in a GWT 2.1.0 project:

public void onSubmit (FormPanel.SubmitEvent event)
{
// This event is fired just before the form is submitted. We can take
// this opportunity to perform validation.
String filename = gDataFileUploader.getFilename ();
if (filename.length () == 0)
{
showAlert ("Need a valid File Name in before we can upload a
file!");
event.cancel ();
}
}

Unfortunately, although it is called, the cancel () call doesn't stop
the submit from happening, and doesn't stop onSubmitComplete from
being called. Is this a GWT bug or a browser bug? (FireFox 3.6)

TIA,

Greg

Jim Douglas

unread,
Jan 5, 2011, 3:41:15 PM1/5/11
to Google Web Toolkit
I'm doing pretty much exactly that with my FileUpload widget, and it
works ok here:

/*
* User clicked the Chooser OK button.
*/
public void onSubmit(SubmitEvent p_event)
{
if (m_progressUpdateTimer != null)
{
// Upload is already in progress
p_event.cancel();
return;
}
.... several more validation checks ....

Greg Dougherty

unread,
Jan 5, 2011, 4:15:00 PM1/5/11
to Google Web Toolkit
GWT 2.0 or 2.1?

Jim Douglas

unread,
Jan 5, 2011, 5:09:09 PM1/5/11
to Google Web Toolkit
Hmm, I would hope it wouldn't matter, but 2.0.3; we need to coordinate
several developers and build systems to move to 2.1.1, and everyone's
been away for Christmas & New Years.

Greg Dougherty

unread,
Jan 7, 2011, 2:54:27 PM1/7/11
to Google Web Toolkit
Ah, because under 2.0 it worked for me, too.

Thomas Broyer

unread,
Jan 7, 2011, 6:57:16 PM1/7/11
to google-we...@googlegroups.com
IIRC, it could happen if you have a SubmitButton and you, in addition, explicitly call form.submit() from a ClickHandler attached to the SubmitButton (which is useless, but not harmless in this case: one of the 2 submits –SubmitButton default action, and explicit form.submit()– is not cancellable).
Other than that (a coworker was in the process of replacing Button+form.submit with SubmitButton but forgot to remove the ClickHandler), I've never faced that issue.

David Pinn

unread,
Jan 12, 2011, 12:04:24 AM1/12/11
to google-we...@googlegroups.com
I'm seeing this behaviour in GWT 2.1.1. 

I am explicitly cancelling the SubmitEvent; yet the following error message is displayed:

"GWT module 'blahblah.MyApp' may need to be (re)compiled"

I've been unable to completely diagnose the problem, but this might be a clue:

The FormPanel class has this method

  private boolean fireSubmitEvent() {
    FormPanel.SubmitEvent event = new FormPanel.SubmitEvent();
    fireEvent(event);
    return !event.isCanceled();
  }

Ultimately, the result of !event.isCanceled() propagates out to this code in FormPanelImpl.java

 form.onsubmit = $entry(function() {
      // Hang on to the form's action url, needed in the
      // onload/onreadystatechange handler.
      if (iframe)
        iframe.__formAction = form.action;
      return listener.@com.google.gwt.user.client.ui.impl.FormPanelImplHost::onFormSubmit()();
    }); 

That's as far as I get with the diagnosis. I cannot see where the event cancellation prevents the form being submitted. 
 

Thomas Broyer

unread,
Jan 12, 2011, 5:54:11 AM1/12/11
to google-we...@googlegroups.com
form.onsubmit = function() { return false; } cancels the default action (i.e. actually send the form data to the server).

balkanski

unread,
Jan 29, 2011, 2:03:18 PM1/29/11
to Google Web Toolkit
I have just found the solution for GWT 2.1.1 version. Here it is:

First you do not use a 'SubmitButton' in your FormPanel, because it
turns out that it causes all the troubles.

Use a 'Button' instead and add a 'ClickHandler' to it, and call
'your_form.submit()' inside the 'onClick()' method.

You will find that after using 'Button' instead of 'SubmitButton' all
the input fields in the form has lost their
initial 'submit on Enter' behavior, which was observed at the
beginning.

Now attach to every input field a 'onKeyDown' handler(if you want, of
course) and check the event's native key code upon 'ENTER', and if yes
- submit the form.

That is all, now the 'event.cancel()' part in the form's SubmitHandler
is perfectly working.

Thomas Broyer

unread,
Jan 31, 2011, 11:50:30 AM1/31/11
to google-we...@googlegroups.com
Given that it apparently only fails in DevMode [1], I don't think it's worth the trouble. YMMV.


Honza Rames

unread,
Apr 18, 2013, 3:30:51 AM4/18/13
to google-we...@googlegroups.com
A little out-of-time response but since this problem still exists in 2.5 I think it's worth sharing another solution. 

If you put "javascript:" into form's action attribute, nothing will happen even if the event is not canceled. I know this isn't helpful in cases where you need to validate the form, but in case you (like me) are preventing the form submit to send the data using GWT RPC instead of the POST to take advantage of the browser's password remembering feature (I'm wrapping FormPanel around already existing form) this is actually helpful.

Thomas Broyer

unread,
Apr 18, 2013, 5:13:25 AM4/18/13
to google-we...@googlegroups.com


On Thursday, April 18, 2013 9:30:51 AM UTC+2, Honza Rames wrote:
A little out-of-time response but since this problem still exists in 2.5 I think it's worth sharing another solution. 

If you put "javascript:" into form's action attribute, nothing will happen even if the event is not canceled. I know this isn't helpful in cases where you need to validate the form, but in case you (like me) are preventing the form submit to send the data using GWT RPC instead of the POST to take advantage of the browser's password remembering feature (I'm wrapping FormPanel around already existing form) this is actually helpful.

Last time I tried, you had to let the submission go through, so you'd have to use action="javascript:foo()" and expose a method as $wnd.foo via JSNI where you'd do your RPC.

Honza Rames

unread,
Apr 22, 2013, 6:06:31 AM4/22/13
to google-we...@googlegroups.com
I tried this just out of curiosity last week and it worked (in FF I didn't test any other browsers). But since I'm using SuperDev mode this issue isn't a problem ;-)
Reply all
Reply to author
Forward
0 new messages