badbishop
badbishop

Reputation: 1648

(Unnecessary) markup in a Wicket form

While generating a simplistic form page using Wicket (version 7.5.0), I'm getting extra markup which seems unnecessary (a hidden field placed into a <div> with inline CSS):

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
  <head>
    <meta charset="utf-8" />
    <title>Apache Wicket Quickstart</title>
    <link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:regular,bold' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="mystyle.css" type="text/css" media="screen" title="Stylesheet"/>
  </head>
  <body>
    <form method="post" wicket:id="ItemForm" id="ItemForm1" action="./tf?1-1.IFormSubmitListener-ItemForm">
      <div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden">
        <input type="hidden" name="ItemForm1_hf_0" id="ItemForm1_hf_0" />
      </div>
      <p>
        <label for="name">
          <span>Item name:</span>
        </label>
        <input type="text" name="p::name" wicket:id="name" value="">
      </p>
      <p>
        <label for="price">
          <span>Item price:</span>
        </label>
        <input type="text" name="price" wicket:id="price" value="0">
      </p>
      <section>
        <input type="submit" value="Submit">
      </section>
    </form>
  </body>
</html>

The relevant Java class is:

// Package name and imports omitted    
public final class ItemFormPage extends WebPage {

      @EJB(name = "ejb/item")
      Item it;

      public ItemFormPage() {
        Form f = new Form("ItemForm") {
          @Override
          public void onSubmit() {
            setResponsePage(new ItemDisplay());
          }
        };

        f.setDefaultModel(new CompoundPropertyModel(it));
        f.add(new TextField("name"));
        f.add(new TextField("price"));
        add(f);
      }
    }

I'm new to Wicket, as is probably evident from the code. Is there a way to avoid generating the aforementioned seemingly unnecessary markup? In other words, am I missing something or should I report a bug?

Upvotes: 0

Views: 284

Answers (1)

Roman Puchkovskiy
Roman Puchkovskiy

Reputation: 11835

This hidden input is used to submit the form with anchor-based components like SubmitLink.

For example, you have a Form and you want to have two ways to submit it (with different 2 buttons):

Form<Void> form = new Form<Void>("form") {
    @Override
    protected void onSubmit() {
        // central form onSubmit
    }
};

SubmitLink submitter1 = new SubmitLink("submitter1") {
    @Override
    public void onSubmit() {
        System.out.println("submitter 1 called");
    }
};
form.add(submitter1);

SubmitLink submitter2 = new SubmitLink("submitter2") {
    @Override
    public void onSubmit() {
        System.out.println("submitter 2 called");
    }
};
form.add(submitter2);

When you click any of the two submitters, its name will be put to that input, and Wicket will find the correct SubmitLink component and call its onSubmit().

Upvotes: 3

Related Questions