0

need help for intbox constraint

asked 2010-07-14 01:45:16 +0800

sarszk gravatar image sarszk
102 1 3

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 :(

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2010-07-14 01:57:31 +0800

mikelara gravatar image mikelara
144
www.mextisa.com.mx

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

link publish delete flag offensive edit

answered 2010-07-14 02:28:51 +0800

Arsen gravatar image Arsen
384 5

You can use Custom constraint where you have full control over validation error message.

link publish delete flag offensive edit

answered 2010-07-14 02:59:11 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-07-14 03:02:23 +0800

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

link publish delete flag offensive edit

answered 2010-07-14 04:01:52 +0800

sarszk gravatar image sarszk
102 1 3

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>

link publish delete flag offensive edit

answered 2010-07-14 04:05:16 +0800

sarszk gravatar image sarszk
102 1 3

Hi Stephan, thanks for solution.
But I need to make it on zul page, not on other class(Composer etc). My currently problem is as I posted upper. Please help me.

link publish delete flag offensive edit

answered 2010-07-14 04:17:00 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-07-14 04:18:53 +0800

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

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-07-14 01:45:16 +0800

Seen: 789 times

Last updated: Jul 14 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More