-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
Is was wondering if anyone knows how to declaratively specify that a Checkbox must be checked, or a Radiogroup must have a selected item? Intuitively I would have thought that adding a constraint="no empty" seemed like a good way to achieve this, however this doesn't work as these components don't except constraints.
I realize I could do this in the event handler and just throw an WrongValueException from there and that would probably work. However this is part of a fairly complex, large application and I would really like to have some uniform way of handling input validation and constraint="" and regular expressions fit the bill perfectly. Otherwise we would need to roll our own validation scheme which kind of seems to defeat the purpose of zk having a built-in one in the first place.
Any help is appreciated.
Kind regards,
Mikkel Løkke
<radiogroup constraint="no empty">
<radio label="Apple" ></radio>
<radio label="Orange" ></radio>
<radio label="Banana" ></radio>
</radiogroup>
Message - > Method setConstraint not found for class org.zkoss.zul.Radiogroup
In another words, the component "radiogroup" dont have the parameter "constraint" in your implementation. How I can introduce this functionality.
Extends de radiogroup? and later what?
I Want the "Hint".
PUSH!
Hi rron,
Yes you can use a custom java class extends radiogroup to achieve this,
e.g.
test.zul
<window title="Radiobox & Radio Demo" width="200px" border="normal"> <vbox> <radiogroup id="rg" use="test.CustomRadiogroup" constraint="no empty" onCheck=""> <radio label="Apple" /> <radio label="Orange" /> <radio label="Banana" /> </radiogroup> You have selected : <label id="fruit" style="color:red" /> <button label="click to refresh page"> <attribute name="onClick"><![CDATA[ // try get label of selected item // true denotes should check the constraint String label = rg.getSelectedItem(true).getLabel(); System.out.println(label); // refresh page Executions.getCurrent().sendRedirect(""); ]]></attribute> </button> </vbox> </window>
CustomRadiogroup.java
package test; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.WrongValueException; import org.zkoss.zul.Radio; import org.zkoss.zul.Radiogroup; public class CustomRadiogroup extends Radiogroup { private String _constraint; public void setConstraint (String constraint) { _constraint = constraint; } // overload instead of override since // getSelectedItem () used in original Radiogroup public Radio getSelectedItem (boolean checkConstraint) throws WrongValueException { Radio radio = super.getSelectedItem(); System.out.println(radio); if (radio == null) { if (_constraint != null && _constraint.contains("no empty")) { String msg = "should select one radio item"; System.out.println(" constraint "); throw new WrongValueException(this, msg); } } return radio; } }
Regards,
Ben
Asked: 2010-07-06 08:32:43 +0800
Seen: 903 times
Last updated: Nov 19 '12