Erik Nelson
Erik Nelson

Reputation: 175

Is it possible to include markup with a custom form in wicket?

I'm trying to implement a form that I can reuse for adding or editing an object. I've noticed if I define a class extending Form that I have to declare the markup for all the form components in the HTML of the page where I'm creating an instance of this form.

I've tried placing a corresponding HTML file with my custom Form class in its package, but it doesn't pick up the fact that the HTML goes with the Java class.

Basically I want the form to look exactly the same, but just to be able to override the onSubmit method of the form so I can use different values when I call setResponsePage for the form.

Upvotes: 2

Views: 1103

Answers (2)

Xavi López
Xavi López

Reputation: 27880

Just to add to @DonRoby 's fine comments, you can look at it this way:

  • You can only provide Markup for those classes extending Page, or WebMarkupContainerWithAssociatedMarkup, such as Panel, Border, Fragment.
  • The other classes, such as TextField, or Form just output their markup possibly based on the state of the instance, through their onComponentTag(), onComponentTagBody() methods and others. TextField outputs <input ...>, and Form outputs <form ....> _formbody_ </form>.

As Don already pointed out, the way to go is to embed this Form inside a Panel. You can always delegate the Form.onSubmit() to a custom method in the wrapping Panel and override it as necessary.

Upvotes: 2

Don Roby
Don Roby

Reputation: 41137

Wicket's Form doesn't support the same pattern of aligning with corresponding markup that many other wicket component classes do.

When I want to do something like this, I wrap the form in a Panel subclass, put the form markup in the corresponding markup for that class, and can then include the panel in multiple pages without having to duplicate the markup.

If you have multiple forms with the same markup, you can also either pass the form itself into the panel as a parameter, or have logic in the panel code that decides which variant to use.

Another possibility for your specific purpose is to pass the response page as a parameter on creating the form and then if that's the only difference you don't need to create two forms.

Upvotes: 6

Related Questions