In the past I have built labels for my form like this:
<label wicket:for="name"><wicket:label><wicket:message key="name"></wicket:message></wicket:label>:</label><input wicket:id="name" type="text"/>
Do I still need to use the wicket:label
tag? I am not using wicket:label
in wicket 7 and it seems to work fine. I may not be understanding the purpose of using wicket:label. It seems like wicket:label
is just additional markup. Below is what I am doing now. Is this correct?:
<label wicket:for="name"><wicket:message key="name"></wicket:message>:</label><input wicket:id="name" type="text"/>
This example is related to Wicket XHTML tags
Apache Wicket uses models to separate the domain layer from the view layer in your application and to bind them together. Components can retrieve data from their model, and convert and store data in the model upon receiving an event.
<wicket:enclosure> - (since 1.3) Useful for markup whose visibility depends on visibility of surrounded component.
Have a look at the JavaDoc of AutoLabelResolver
and AutoLabelTextResolver
.
The <label wicket:for="name">
is handled by AutoLabelResolver
. It links the HTML label
tag to the HTML form component (in your case the input
tag) by filling in the correct ID in the HTML for
attribute of the label. It also adds css classes to the label tag for for example errors, so you can style the text in the label tag in case of an error.
The <wicket:label>
has two purposes. If you give it a value either by the key
attribute (as you did) or by having some text between the tags, the text is set as the label model of the Java FormComponent, which then is used in validation messages like this '${label}' is required.
(see LabeledWebMarkupContainer#setLabel
and LabeledWebMarkupContainer#getLabel
).
If you don't assign any text to the <wicket:label>
tag, then it is used as output. That means the value of the label model of your Java FormComponent is used to replace the tag.
If you have no <wicket:label>
in the HTML markup and no label model set in your Java code, then your Java FormComponent will have an empty label model and Wicket falls back to using the Wicket ID as label. So depending on how your Wicket IDs look, you will get validator messages like 'user_name' is required.
instead of something nice looking like 'User name' is required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With