You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Juan Gabriel Arias <ju...@gmail.com> on 2008/02/12 13:36:37 UTC

Re: Form/Enter Key Problem

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 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