Search code examples
javawicketwicket-1.6

Wicket: Preventing form submission by a TextField within a Wizard step


Question relates to Wicket 1.6

I have a wizard step, which includes a Textfield component. When I press the Enter key, this is being handled by the default button of the Wizard bar ('Next'), and it advances to the next step in the Wizard. I don't want this to happen. When I hit Enter on the Textfield I just want the value to be updated, but remain on the same page.

I tried overriding the onBeforeRender() method of my Wizard class, which as you can see sets the default button of the containing form to null. However this now results in the 'Prev' button being triggered when I hit Enter, so the wizard goes back to the previous step.

public class ConfigurationWizard extends Wizard {

   ....

   @Override
   protected void onBeforeRender()
    {
        super.onBeforeRender();
        Component buttonBar = getForm().get(BUTTONS_ID);
        if (buttonBar instanceof IDefaultButtonProvider)
        {
            getForm().setDefaultButton(null);
        }
    }
}

So the basic question is, how do I disable the default button behaviour of the Wizard?


Solution

  • This has nothing to do with the wizard buttons.

    The TextField <input> is doing a form submit when the Enter key is pressed. This is standard behaviour for the <input> element.

    The solution is to catch the Enter key press for the <input> and prevent the default behaviour This bit of javascript magic does the trick for me:

       <script type="text/javascript">
       $(document).ready(function() {
                 $("#gridDiv").delegate("input","keypress",function(e){
                     if(e.originalEvent.keyCode == 13){
                         e.preventDefault();
                     }
                  });
              });
       </script>
    

    where 'gridDiv' is the id of the <div> containing the TextField