-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello all,
I need your help again. I want to create field and validate on it once user input the data. But I have difficult how to make 2 constraint in the same time, so it is mandatory and must insert by integer. So, when the user input data text type, will display alert "data should integer"
Please help me.
here my code
<intbox constraint="no empty" />
please somebody help :(
Hi.
please be aware that intbox does not allow to put any text input. Regarding to the other restriction, you can do an alert("data is mandatory on this field"); on the onchange event of the intbox. Hope this help you.
Mike
You can use Custom constraint where you have full control over validation error message.
You can do that with expressions and custom constraints.
zul
<textbox constraint="com.test.OnlyEmptyAndIntegerConstraint" />
java
Textbox searchField = new Textbox();
searchField.setConstraint(new OnlyEmptyAndIntegerConstraint());
/** * Constraint for comparing the value from a textbox.<br> * Throws an error message if not empty or only numbers ( 0-9 ). Used in the * PayConditions Searchbox for checking that the SearchToken is only integers or * empty<br> * * @author sgerth * */ public class OnlyEmptyAndIntegerConstraint implements Constraint, java.io.Serializable { private static final long serialVersionUID = 4052163775381888061L; public OnlyEmptyAndIntegerConstraint() { super(); } @Override public void validate(Component comp, Object value) throws WrongValueException { if (comp instanceof Textbox) { // cast the value to String and trim it. final String enteredValue = ((String) value).trim(); // check if not empty if (!enteredValue.isEmpty()) { // check if not allowed signs if (!enteredValue.matches("(([0-9]+)?)+")) { throw new WrongValueException(comp, Labels.getLabel("message.error.OnlyNumbersOrEmptyAllowed")); } } } } }
best
Stephan
Hi Arsen,
Thanks for your link recommended, I get success to follow that tutorial. But I have problem, how to implement it if I want to validate more than one field that same validation.
here my code, please help me
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<zk>
<zscript>
<![CDATA[
class MyConst implements Constraint, CustomConstraint {
//Constraint//
public void validate(Component comp, Object value) {
if (value == null || value.equals(""))
throw new WrongValueException(comp, "Data must be not empty");
if (((java.math.BigDecimal) value).intValue() < 0) {
throw new WrongValueException(comp, "Data must be not negative");
}
}
//CustomConstraint//
public void showCustomError(Component comp, WrongValueException ex) {
errorMsg.setValue(ex != null ? ex.getMessage() : "");
}
}
Constraint ctt = new MyConst();
]]>
</zscript>
<grid fixedLayout="false">
<columns>
<column label="" width="300px" zclass="gridnonlist" />
<column label="" width="10px" zclass="gridnonlist" />
<column label="" width="250px" zclass="gridnonlist" />
<column label="" width="" zclass="gridnonlist" />
</columns>
<rows>
<row zclass="gridnonlist">
<hbox>
<label value="Value 1" />
<label value="*" style="color:red" />
</hbox>
<label value=":" />
<decimalbox maxlength="30" id="dcb1"
format="##,##0.0000000000" width="200px" constraint="${ctt}" />
<label id="errorMsg" style="color:red" />
</row>
<row zclass="gridnonlist">
<hbox>
<label value="Value 1" />
<label value="*" style="color:red" />
</hbox>
<label value=":" />
<decimalbox maxlength="30" id="dcb2"
format="##,##0.0000000000" width="200px" constraint="${ctt}" />
<label id="errorMsg2" style="color:red" />
</row>
</rows>
</grid>
</zk>
Than write the codes in your zul-file.
You can modify the constructor for your custom constarint, so that he can handle two components. You can read about that in our Zksample2 documentation Chapter 13 here. (with codes)
best
Stephan
Asked: 2010-07-14 01:45:16 +0800
Seen: 789 times
Last updated: Jul 14 '10