Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.errai.ui.client.widget;
  
  
  import  com.google.gwt.dom.client.Document;
  import  com.google.gwt.dom.client.FormElement;
  import  com.google.gwt.dom.client.IFrameElement;
  import  com.google.gwt.dom.client.Style.Display;
  import  com.google.gwt.event.dom.client.ClickHandler;
 import  com.google.gwt.safehtml.client.SafeHtmlTemplates.Template;
 import  com.google.gwt.user.client.ui.Composite;

Base class for Templated widgets that require native form support for asynchronous GET or POST requests.

Calls to AbstractForm.submit() trigger a form submission that is null-routed and targets a hidden iframe.

The typical usage will be for login forms, where calling AbstractForm.submit() after a successful login will prompt the browser to remember a user's credentials.

If you use an AbstractForm and still encounter issues getting proper browser form support:

  • make sure that your text-fields are actual form inputs
  • make sure the UI "submit" button triggers a ClickHandler that calls AbstractFormsubmit()
  • make sure the UI "submit" button does not directly trigger submission (i.e. it should not have type="submit"

    Author(s):
    Max Barkley <mbarkley@redhat.com>
 
 public abstract class AbstractForm extends Composite {
 
   public static final String DEFAULT_FORM_ACTION = "0.0.0.0";
   public static final String ERRAI_FORM_FRAME_ID = "ERRAI-FORM-FRAME";
 
   private IFrameElement iFrame;
 
   private void setupIFrame() {
      = getOrMakeIFrame();
 
     .getStyle().setDisplay(Display.NONE);
     final String uniqueId = ;
     .setId(uniqueId);
     .setName(uniqueId);
 
     Document.get().getBody().appendChild();
   }
 
   private void prepareFormForSubmission(final FormElement form) {
     form.setMethod("post");
     form.setTarget(.getName());
     form.setAction(getFormAction());
   }

  
Subclasses may override this method if they want the form to submit to a different url than AbstractForm.DEFAULT_FORM_ACTION.

Returns:
The value that will be set to the AbstractForm.getFormElement() action attribute before submission.
 
   protected String getFormAction() {
     return ;
   }
 
   private IFrameElement getOrMakeIFrame() {
     IFrameElement iFrame = null;
     try {
       iFrame = (IFrameElement) Document.get().getElementById();
     }
     catch (Exception e) {
     }
 
     if (iFrame == null)
       iFrame = Document.get().createIFrameElement();
 
     return iFrame;
   }

  
Submit the form returned from AbstractForm.getFormElement(). Before the form is submitted, it will be modifed so that it targets a hidden iframe with the id AbstractForm.ERRAI_FORM_FRAME_ID with the action set to the return value of AbstractForm.getFormAction().
 
  public void submit() {
    final FormElement form = getFormElement();
    form.submit();
  }

  

Returns:
The form element that will be submitted when AbstractForm.submit() is called.
  protected abstract FormElement getFormElement();
New to GrepCode? Check out our FAQ X