Using Forms

TODO

Start with an example using the pojo from 4

Subclass wicket.markup.html.WebPage and in the constructor initialize and add the required Form implementation.

public class EditCustomerPage extends WebPage {
    public EditCustomerPage() {
        add(new CustomerForm("customerForm", new Customer()));
    }
}

Subclass wicket.markup.html.form.Form and in the constructor add the necessary form elements.
These will be bound to the individual html form elements through the wicket:id attributes.

public class CustomerForm extends Form {
    public CustomerForm(String name, Customer customer) {
        super(id, new CompoundPropertyModel(customer));
        add(new TextField("name"));
    }
}

The html form itself must contain a wicket:id attribute that matches the Form added to the WebPage. Each form element must also have a corresponding wicket:id to each FormComponent added to the Form. In this example the form is bound to the "customerForm" Form instance and the text input is bound to the "name" TextField. There is also a single submit button that is not bound to any component. Wicket automatically provides the submission handling functionality through the Form base class implementation. Button components only need to be added to the Form if multiple buttons are required.

<form wicket:id="customerForm">
    name: <input type="text" wicket:id="name"/><br>
    <input type="submit"/>
</form>

Updating the model

What happens when the form is submitted? Describe how the automatic updating of pojo's when property models are used. Describe how to use buttons and a button-less form.

Validation

NO LONGER VALID AS OF RC1, FORM parent method doesn't take a FeedbackPanel

Describe how validation works and how to implement custom validation. Also give a short explanation of how to display messages.

Adding RequiredValidator to a form element will allow only non-null and non-empty values to pass validation successfully.

public CustomerForm(String name, Customer customer, IFeedback feedback) {
    super(id, new CompoundPropertyModel(customer), feedback);
    add(new TextField("name").add(RequiredValidator.getInstance()));
}

For convenience the class RequiredTextField is provided. This TextField automatically includes a RequiredValidator.

public CustomerForm(String name, Customer customer, IFeedback feedback) {
    super(id, new CompoundPropertyModel(customer), feedback);
    add(new RequiredTextField("name"));
}

To provide validation feedback a FeedbackPanel can be instantiated. This should be added to the page (for display) and associated with the form by passing it into the form constructor.

public EditCustomerPage() {
    FeedbackPanel feedback = new FeedbackPanel("feedback");
    add(feedback);
    add(new CustomerForm("customerForm", new Customer(), feedback));
}

The FeedbackPanel can then be added to the html with a wicket:id span.

<span wicket:id="feedback">feedback will be here</span>

Finally, create a .properties file in the same directory as your html template and class file using the same naming conventions. In this case it would be called EditCustomerPage.properties. Use this file to define the error messages for each input in the form.

formId.inputId.ValidatorClassName=Your custom message.

Conversion

Describe how conversion works (when using property models) and how to implement custom conversion.

Form components

Give a list with a short description of the form components that are available in core.

  • No labels

1 Comment

  1. I'd really love to see this page finished. Some of the form lifecycle in Wicket was a bit surprising to me, such as rawInput being cleared out after validation.