You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by yesotaso <ye...@yahoo.com> on 2012/07/05 09:42:40 UTC

Re: how to connect elements - label and input (checkbox)

I am not exactly sure if it is what you want but I used a decoretor for
labeling task.
public class SimpleDecorator extends Behavior implements
IAjaxRegionMarkupIdProvider{
	@Override
	public String getAjaxRegionMarkupId(Component component){
// This method is overriden from IAjaxRegionMarkupIdProvider see its
@JavaDoc for details
		return component.getMarkupId() + "_fd";
	}

	@Override
	public void bind(Component component) {
		component.setOutputMarkupId(true);
	}

	@Override
	public void beforeRender(Component component) {
		FormComponent&lt;?> fc = (FormComponent&lt;?>) component;
		Response r = component.getResponse();
		String label = (fc.getLabel() != null) ? fc.getLabel().getObject()	: null;

		if(fc.getParent() instanceof FormComponent&lt;?>) {
			label = null;
		}

		if (label != null) {
			r.write("<label for=\&quot;&quot;);
			r.write(fc.getMarkupId());
			r.write(&quot;\&quot;&quot;);
			if (fc.isValid() == false) {
				r.write(&quot; class=\&quot;error\&quot;&quot;);
			}
			r.write(&quot;>");
			r.write(Strings.escapeMarkup(label));
			if (fc.isRequired()) {
// You can put a "*" or something else if field is required
			}
			r.write("&lt;/label>");
		}
		super.beforeRender(component);
	}
	@Override
	public void afterRender(Component component) {
		Response r = component.getResponse();
		r.write("&lt;/div>");
	}

	@Override
	public void onComponentTag(Component component, ComponentTag tag) {
// If you want to put some css stuff etc for more striking invalid
visuals...
		FormComponent&lt;?> fc = (FormComponent&lt;?>) component;
		if (fc.isValid() == false) {
			String cl = tag.getAttribute("class");
			if (cl == null) {
				tag.put("class", "error");
			} else {
				tag.put("class", "error " + cl);
			}
		}
	}
}

And usage is 
... add(new TextField("someId").setLabel(Model.of("I'm a label")).add(new
SimpleDecorator()))

I believe this is from _Wicket Cookbook_ or _Wicket In Action_ I cant
recall. For checkbox etc you can omit invalid input etc... but idea is the
same.
You can also omit adding behavior and add it later via visitor. I hope it
helps.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/how-to-connect-elements-label-and-input-checkbox-tp4650304p4650386.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org