-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi all,
I am trying to construct a (simple) constraint for use with a couple of intbox elements. My class implements Constraint and throws a "WrongValueException" in its validate method.
So far so good...
While the constraint itself works okay, there are two problems with it:
1) The error message does not look like the standard constraint error messages. Likewise, I'd like the border of the erroneous intbox to turn red, just like when the standard constraint kicks in.
2) My own constraint only "kicks in" when the value in the intbox has been changed and the focus is lost. If I go back to the same intbox after the alert, do nothing and then leave the box the warning does not appear again...
What do I need to implement to get the same look and behavour as the standard constraints (if possible)?
One thing I tried was to extend the "SimpleConstraint" (with "no empty" set as the default behavour). This results in "my" constraint message shown when "my" error is met, and the SimpleConstraints behavour when I leave the field empty?
Is there any method I should override that I have missed?
Regards,
Rico
Hi Rico, if this pic shows what you want?
The code for this is following:
package de.forsthaus.webui.util; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.WrongValueException; import org.zkoss.zul.Constraint; import org.zkoss.zul.Messagebox; import org.zkoss.zul.Textbox; /** * Constraint for comparing the value-strings from a textbox with a string.<br> * Throws an error message if not equals. Used in the userDialog for checking * that the reTyped password is same as first written password.<br> * * @author sge(at)forsthaus(dot)de * @changes 07/24/2009: sge changes for clustering.<br> * * */ public class NoEmptyAndEqualStringsConstraint implements Constraint, java.io.Serializable { private static final long serialVersionUID = 4052163775381888061L; private transient Component compareComponent; public NoEmptyAndEqualStringsConstraint(Component compareComponent) { super(); this.compareComponent = compareComponent; } @Override public void validate(Component comp, Object value) throws WrongValueException { if (comp instanceof Textbox) { String enteredValue = (String) value; String comparedValue = null; if (compareComponent instanceof Textbox) { comparedValue = ((Textbox) compareComponent).getValue(); } if (enteredValue.isEmpty()) { throw new WrongValueException(comp, Labels .getLabel("message.error.CannotBeEmpty")); } else if (!enteredValue.equals(comparedValue)) { throw new WrongValueException(comp, Labels .getLabel("message.error.RetypedPasswordMustBeSame")); } } } }
settings in code
... usrPassword.setConstraint("NO EMPTY"); usrPasswordRetype.setConstraint(new NoEmptyAndEqualStringsConstraint(usrPassword)); ...
get the Message
... usrPassword.getValue(); usrPasswordRetype.getValue(); ...
best
Stephan
I can't get a custom constraint to work at all. Wondering if you guys could help instead of me making a new thread. I can't find anything else about custom constraints.
I am trying to do what the documentation says to do.
"You create a Java class to contain your constraint, say my.EmailValidator, then reference it in the markup: "
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<textbox constraint="${c:new('my.EmailValidator')}"/>"
I have this:
<textbox id="facilityNumber cols="40" constraint="${c:new('CustomConstraint')}" value="@{facility.facilityNumber}" />
and a simple class(this was just to test to see if this could work):
package com.fedex.ground.trm.ui.util;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Textbox;
public class CustomConstraint implements Constraint{
public void validate(Component component, Object value) throws WrongValueException {
if (component instanceof Textbox) {
String enteredValue = (String) value;
if (enteredValue.trim().length() < 4) {
throw new WrongValueException(component, Labels.getLabel("Value too short"));
}
}
}
}
and I get errors on "c:new". I am not sure how to setup a custom constraint and I've been at this for hours, and the code above, I don't understand. the "settings in code" and "get the message", where are these located between the text box and the constraint?
I use this way of setting up the custom constraint:
<zscript> CustomConstraint cc = new CustomConstraint(); </zscript> <textbox id="facilityNumber" cols="40" constraint="${cc}" value="@{facility.facilityNumber}" />
Asked: 2009-11-04 09:43:52 +0800
Seen: 6,523 times
Last updated: Dec 22 '09