You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ballist1c <le...@msn.com> on 2007/12/19 00:32:14 UTC

Form/Enter Key Problem

Hey guys, 

I have created the following form (code below).

The form works fine, when i enter a value into the txt field and mouse click
on the AjaxFormSubmitLink... BUT if i type something into the TextField and
press the enter key, the page loads 'something'... but does nothing.... and
the form is broken till i do a hard refresh, usually i close the browser and
start it up again.   The application itself doesn't crash, and all the other
AJAX on the page works fine.

I want to know if anyone has had this problem before.  I am pretty stuck on
this one at the moment.

One hack solution i was wondering if someone can help me out with, is to
disable the auto-submit on Enter Key press... anyone know how to do that? 


Thanks guys!!!
LEo1


public class SearchInputPanel extends Form
{
   TextField basicSearchString;
   DropDownChoice dodginess;
   AjaxFormSubmitLink addButton;
   Input input;

   /**
    * Creates a new instance of FormPanel
    */
   public SearchInputPanel(String id, MarkupContainer parent)
   {
      super(id, parent);
   }

   @Override
   public void setupComponents()
   {
      input = new Input();
      basicSearchString = new TextField("searchString", this);
      dodginess = new DropDownChoice("dodginess", this, Arrays.asList(new
String[]{"1", "2", "3", "4", "5"}));

      addButton = new AjaxFormSubmitLink("add", this)
      {
         public void onSubmit(AjaxRequestTarget target)
         {
            //MessageManager.getInstance().getLatestMessages();
            //((BigBrotherCookieSession)
getJumbuckCookieSession()).setSearchResults(MessageManager.getInstance().getNewMessages());
            target.addComponent(findParent(BasicSearchPanel.class));
         }

         public Form getJumbuckForm()
         {
            return SearchInputPanel.this.getForm();
         }
      };
   }

   @Override
   public void setupModels()
   {
      setModel(new CompoundPropertyModel(input));
   }

   @Override
   public void onSubmit()
   {
      BigBrotherCookieSession session = (BigBrotherCookieSession)
getJumbuckCookieSession();
      session.addSearchString(input.getSearchString(),
input.getDodginess());

      input.setSearchString("");
   }

   private static class Input implements IClusterable
   {
      private String searchString = "";
      private Long dodginess = 1L;

      public String getSearchString()
      {
         return searchString;
      }

      public void setSearchString(String searchString)
      {
         this.searchString = searchString;
      }

      public Long getDodginess()
      {
         return dodginess;
      }

      public void setDodginess(Long dodginess)
      {
         if (dodginess == null)
            this.dodginess = 1L;
         else
            this.dodginess = dodginess;
      }
   }
}

-- 
View this message in context: http://www.nabble.com/Form-Enter-Key-Problem-tp14408121p14408121.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Form/Enter Key Problem

Posted by SantiagoA <s....@gmx.de>.
'Enter' is bound to the default Button on your page. You can prevent the
submit by adding some javascript, which intercepts the onKeyPress() 

public static final String JS_SUPPRESS_ENTER = "if(event.keyCode==13 ||
                                     window.event.keyCode==13){return
false;}else{return true;}";
  

public void onComponentTag(Component component, ComponentTag tag) {
    tag.put("onkeydown", JS_SUPPRESS_ENTER);
    tag.put("onkeypress", JS_SUPPRESS_ENTER);
}

hope that helps.
Santiago




Ballist1c wrote:
> 
> Hey guys, 
> 
> I have created the following form (code below).
> 
> The form works fine, when i enter a value into the txt field and mouse
> click on the AjaxFormSubmitLink... BUT if i type something into the
> TextField and press the enter key, the page loads 'something'... but does
> nothing.... and the form is broken till i do a hard refresh, usually i
> close the browser and start it up again.   The application itself doesn't
> crash, and all the other AJAX on the page works fine.
> 
> I want to know if anyone has had this problem before.  I am pretty stuck
> on this one at the moment.
> 
> One hack solution i was wondering if someone can help me out with, is to
> disable the auto-submit on Enter Key press... anyone know how to do that? 
> 
> 
> Thanks guys!!!
> LEo1
> 
> 
> public class SearchInputPanel extends Form
> {
>    TextField basicSearchString;
>    DropDownChoice dodginess;
>    AjaxFormSubmitLink addButton;
>    Input input;
> 
>    /**
>     * Creates a new instance of FormPanel
>     */
>    public SearchInputPanel(String id, MarkupContainer parent)
>    {
>       super(id, parent);
>    }
> 
>    @Override
>    public void setupComponents()
>    {
>       input = new Input();
>       basicSearchString = new TextField("searchString", this);
>       dodginess = new DropDownChoice("dodginess", this, Arrays.asList(new
> String[]{"1", "2", "3", "4", "5"}));
> 
>       addButton = new AjaxFormSubmitLink("add", this)
>       {
>          public void onSubmit(AjaxRequestTarget target)
>          {
>             //MessageManager.getInstance().getLatestMessages();
>             //((BigBrotherCookieSession)
> getJumbuckCookieSession()).setSearchResults(MessageManager.getInstance().getNewMessages());
>             target.addComponent(findParent(BasicSearchPanel.class));
>          }
> 
>          public Form getJumbuckForm()
>          {
>             return SearchInputPanel.this.getForm();
>          }
>       };
>    }
> 
>    @Override
>    public void setupModels()
>    {
>       setModel(new CompoundPropertyModel(input));
>    }
> 
>    @Override
>    public void onSubmit()
>    {
>       BigBrotherCookieSession session = (BigBrotherCookieSession)
> getJumbuckCookieSession();
>       session.addSearchString(input.getSearchString(),
> input.getDodginess());
> 
>       input.setSearchString("");
>    }
> 
>    private static class Input implements IClusterable
>    {
>       private String searchString = "";
>       private Long dodginess = 1L;
> 
>       public String getSearchString()
>       {
>          return searchString;
>       }
> 
>       public void setSearchString(String searchString)
>       {
>          this.searchString = searchString;
>       }
> 
>       public Long getDodginess()
>       {
>          return dodginess;
>       }
> 
>       public void setDodginess(Long dodginess)
>       {
>          if (dodginess == null)
>             this.dodginess = 1L;
>          else
>             this.dodginess = dodginess;
>       }
>    }
> }
> 
> 

-- 
View this message in context: http://www.nabble.com/Form-Enter-Key-Problem-tp14408121p14413071.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Form/Enter Key Problem

Posted by Scott Swank <sc...@gmail.com>.
Another solution, since we have to ignore the enter key within some
FormComponents:


form.visitChildren(FormComponent.class, new IVisitor()
{
	@Override
	public Object component(Component component)
	{
		if (!(component instanceof Button))
			component.add(new DisableEnterKeyBehavior());

		return IVisitor.CONTINUE_TRAVERSAL;
	}
});


public class DisableEnterKeyBehavior extends AbstractBehavior
implements IHeaderContributor
{
	private static final long serialVersionUID = 7123908800158111365L;
	private FormComponent formComp;

	public void renderHead(IHeaderResponse response)
	{
		response.renderJavascript(
				"function ignoreEnterKey(event){return !(event.keyCode==13 ||
window.event.keyCode==13);}",
				"vcomDisableEnterKey");
	}

	@Override
	public final void bind(Component component)
	{
		if (component == null)
			throw new IllegalArgumentException("Argument component must be not null");

		if (formComp != null)
			throw new IllegalStateException(
					"This behavior cannot be attached to multiple components; it is
already attached to component "
							+ formComp + ", but component " + component + " wants to be
attached too");

		if (!(component instanceof FormComponent))
			throw new IllegalArgumentException("This behavior can only be
attached to a FormComponent.");

		formComp = (FormComponent) component;
	}

	@Override
	public final void onComponentTag(Component component, ComponentTag tag)
	{
		if (formComp.isEnabled() && formComp.isEnableAllowed())
		{
			tag.put("onkeypress", "return ignoreEnterKey(event);");
		}
	}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Form/Enter Key Problem

Posted by Juan Gabriel Arias <ju...@gmail.com>.
My workaround was this. The idea is to add a submit behavior to the text
field, and it fires that only when user hits enter.

This code is inside the form's constructor.

searchCriteria = new TextField("searchCriteria", new Model(""));
searchCriteria.add(new AjaxFormSubmitBehavior(this, "onkeypress") {

    protected void onSubmit(AjaxRequestTarget target) {
        //submit code
    }

    protected CharSequence getEventHandler() {
        CharSequence handler = super.getEventHandler();

        String check = "javascript: if (event.keyCode == 13) { ";
        String endCheck = " return false; }";

        return StringUtils.join(new Object[] { check, handler, endCheck });
    }

});

What do you think?

Regards!
Juan

Re: Form/Enter Key Problem

Posted by John Krasnay <jo...@krasnay.ca>.
On Tue, Dec 18, 2007 at 03:32:14PM -0800, Ballist1c wrote:
> 
> Hey guys, 
> 
> I have created the following form (code below).
> 
> The form works fine, when i enter a value into the txt field and mouse click
> on the AjaxFormSubmitLink... BUT if i type something into the TextField and
> press the enter key, the page loads 'something'... but does nothing.... and
> the form is broken till i do a hard refresh, usually i close the browser and
> start it up again.   The application itself doesn't crash, and all the other
> AJAX on the page works fine.
> 
> I want to know if anyone has had this problem before.  I am pretty stuck on
> this one at the moment.
> 

IIRC, when you press Enter, Firefox simulates clicking the first button
on the form, i.e. the (name, value) pair of the button is sent as part
of the POST. However, Internet Explorer *doesn't* do this; it simply
submits the form as if no button has been pressed. In this case, Wicket
wouldn't know to route your post to your AjaxFormSubmitLink.onSubmit.

I would try overriding Form.onSubmit in your form class instead.

> One hack solution i was wondering if someone can help me out with, is to
> disable the auto-submit on Enter Key press... anyone know how to do that? 

Your users might find this annoying. I know I would!

jk

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org