Tutorial :Wicket : Can a Panel or Component react on a form submit without any boilerplate code?



Question:

I am currently evaluating Wicket and I am trying to figure out how things work.

I have a question regarding form submit and panels (or other components). Imagine a custom wicket panel which contains a text field, doing as-you-type validation using ajax. This panel is added to a form. How can the Panel react a form submit (let's say because javascript/ajax is unavailable)?

I am currently only aware of one solution: calling a panel's method inside the Form onSubmit() method. But this seems not like a "reusable" approach here, because I have to add boilerplate code to every form's onSubmit() which contains the panel (and every developer which use the panel must know this).

So here comes my question: Is there any way that a Panel/Component can "detect" a form submit in some way? Or is there any other solution beside this?

Thank you.


Solution:1

Make your panels implement org.apache.wicket.markup.html.form.IFormModelUpdateListener, and the updateModel() method should be called when the containing form is submitted and passes validation.

There's a good example of code using this by one of the wicket authors at the Wicket In Action blog.


Solution:2

Well, you could simply do the following:

Panel{  Form{    onSubmit(){      Panel.this.onSubmit();    }  }    protected void onSubmit(){}    }  

...

This means that any panel that extends your panel need only override the onSubmit and the form no matter what it is in html will call that method. That way you can extend the panel and only override one method for each form.


Solution:3

With regard to form components, the framework handles it for you transparently. Forms are aware of any child form components, even if they haven't been added directly to the parent form.


Solution:4

I would have a Form inside that Panel. This way, you can reuse that Panel without requiring an external Form. As Forms can not be nested inside each other in HTML, Wicket will swap the inner Form(s) into 's transparently, but will make sure that each of the inner Forms takes part of the form processing (validation,..).

You can override the OnSubmit() function of the Form in your Panel. Wicket will call it for you.


Solution:5

what do you mean by "react"? I have only started recently with Wicket, but FWIK, form submit updates the model of a component, and then it calls onSubmit(), which you can override to take special actions beyond that. See Wicket in Action, chapter 6.

After that, the page (and it's components) get re-rendered, using the updated model, so basically, they really "react" on a submit, with quite few lines of code.

For your mentioned case with Component in a Form, have a look at the CompoundPropertyModel.


Solution:6

Implementing IFormSubmitListner and IFormModelUpdateListener shall call the respective methods during a form submit.

However, if you want to do some processing after form submit, I'm afraid you have no choice but to write some boilerplate code yourself.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »