button within a form

66 views
Skip to first unread message

sluramod

unread,
Aug 14, 2006, 6:02:34 PM8/14/06
to Google Web Toolkit
Hello,

I noticed a bug in 1.1.0 release. If you place a button inside
FormPanel, then if you click the button it will submit the form in all
browsers except IE. You can use this code to fix the problem:

package com.whatever.gwt.client;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;

public class FormButton extends Button {

/**
* Creates a button with no caption.
*/
public FormButton() {
super();
fixButton(getElement());
}

/**
* Creates a button with the given HTML caption.
*
* @param html
* the HTML caption
*/
public FormButton(String html) {
super(html);
fixButton(getElement());
}

/**
* Creates a button with the given HTML caption and click listener.
*
* @param html
* the HTML caption
* @param listener
* the click listener
*/
public FormButton(String html, ClickListener listener) {
super(html, listener);
fixButton(getElement());
}

public static native void fixButton(Element e) /*-{
if (e.type == 'submit') {
e.type = 'button';
}
}-*/;

}

Hope this helps,
Alexei

hoosie

unread,
Aug 15, 2006, 1:40:28 AM8/15/06
to Google Web Toolkit
Thanks Alexei! Just ran into this problem, Firefox was submitting
forms twice (or popping up the validation error messages twice). Your
FormButton class completely fixed the problem.

-Ian

dan_lat

unread,
Aug 15, 2006, 11:33:48 AM8/15/06
to Google Web Toolkit
Hi, the above code helped me a lot! thanks v much.

Vivian

unread,
Aug 16, 2006, 3:48:27 AM8/16/06
to Google Web Toolkit

According to the HTML spec
http://www.w3.org/TR/html4/interact/forms.html#edef-BUTTON, the default
for all <button> markup is type="submit" unless otherwise specified,
which is what happens in the Button constructor when it calls
DOM.createButton(). An alternative to using JSNI is to call
DOM.setAttribute(button.getElement(), "type", "button").

-Vivian

dan_lat

unread,
Aug 16, 2006, 11:12:19 AM8/16/06
to Google Web Toolkit
Hi Vivian, I am having the same problem as stated in 'hoosie' message
above, I implemented Alexis FormButton class and this fixed the
problem.. but, the problem has just re-appeared even using the
FormButton Class, do you have any suggestions as to what is causing
this? and any workarounds, thanks

dan_lat

unread,
Aug 17, 2006, 3:52:23 AM8/17/06
to Google Web Toolkit

YUY0x7

unread,
Aug 22, 2006, 6:14:22 PM8/22/06
to Google Web Toolkit
Vivian, the line you're suggesting fails in IE for some reason. The GWT
shell tells me "Object does not support this operation". It works fine
in FF, though. The code posted by sluramod works for both browsers.

Thank you both.

Reply all
Reply to author
Forward
0 new messages