views:

312

answers:

5

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.

A: 

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.

ireddick
I know that the form components are aware of its child. But my question was the other way round: How can a panel react on a form submit?
MRalwasser
A: 

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.

msj121
I am not talking about extending my custom panels. I am talking about using them, without boilerplate code.
MRalwasser
A: 

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.

bert
A: 

Hi, 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.

Ondra Žižka
A: 

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.

Don Roby