Sunday, December 19, 2010

Wicket : Unable to re-submit the form after the form errors

I was working on a login form where two fields were used as usual i.e. login id and password.

I used a form with an AjaxSubmitLink and overrode its onSubmit and onError methods.
I did not use the FeedbackPanel on the form as I used custom labels with some style to show the errors.

in the onError overridden method I did the following :


      protected void onError(final AjaxRequestTarget target, final Form form) {

        if(userEmail.getFeedbackMessage() != null) {
          invalidEmailLabel.setDefaultModelObject(
                          userEmail.getFeedbackMessage().getMessage().toString());
          invalidEmailLabel.setOutputMarkupPlaceholderTag(true);
        }
        if(userPassword.getFeedbackMessage() != null) {                    
           userPassword.getFeedbackMessage().getMessage().toString());

        passwordError.setDefaultModelObject(
                   userPassword.getFeedbackMessage().getMessage().toString()); 
          loginErrorMessage.setVisible(false);
        }


        userPassword.clearInput();
        userPassword.setModelObject("");
        form.add(userPassword);
        target.addComponent(form);
        
      }
    };

This code looks fine. But I faced one issue - After the page is loaded for the first time, If I enter correct data it works fine but as soon as I input the invalid values, the form validation fails and form error occurs. Which is an expected behavior but then again if I give the correct values and try to submit the form, it doest pass me through. I played with ajax a lot but coud not find the solution.

Then I found the cleanupFeedbackMessages() method by doing some search, but in my custom session I had an empty implementation of cleanupFeedbackMessages(), as my custom session extended org.apache.wicket.Session. 
I changed my session's parent class to org.apache.wicket.protocol.http.WebSession; which does not require to override the cleanupFeedbackMessages() method.

Then in my onError method I called the following which solved my problem.


getSession().cleanupFeedbackMessages();

This method saved my life,My ajax implementation worked fine as expected. I was almost going to implement it on page load event. But could not compromise with what I desired.

Finally somebody has said it correctly : "Where is the will , there is the way."

Let me know if this info helped you.

Thanks,
Sanjay :)

No comments:

Post a Comment