-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello,
i tried to write my first custom constrained. Here is the code:
package ui.constraints; import java.math.BigDecimal; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.WrongValueException; import org.zkoss.zul.SimpleConstraint; @SuppressWarnings("serial") public class BidConstraint extends SimpleConstraint { private float minValue; public BidConstraint(int flags, float pMinValue) { super(flags); this.minValue=pMinValue; } @Override public void validate(Component comp, Object value) throws WrongValueException { super.validate(comp, value); float enteredValue = ((BigDecimal)value).floatValue(); if (enteredValue<minValue) throw new WrongValueException(comp,"Mindestens "+minValue+"!"); } }
This works fine with the following script:
<?xml version="1.0" encoding="UTF-8"?> <?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?> <p:zk xmlns:p="http://www.zkoss.org/2005/zul" xmlns:a="http://www.zkoss.org/2005/zk/annotation"> <?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?> <p:window id="bid" title="Test Constraints" border="normal" width="300px" sizable="true" closable="true"> <p:zscript> <![CDATA[ import java.math.BigDecimal; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.WrongValueException; import org.zkoss.zul.SimpleConstraint; import ui.constraints.BidConstraint; BidConstraint myBidConstraint = new BidConstraint( org.zkoss.zul.SimpleConstraint.NO_EMPTY |org.zkoss.zul.SimpleConstraint.NO_NEGATIVE ,(float)1000); ]]> </p:zscript> <p:grid> <p:rows> <p:row> <p:decimalbox constraint="${myBidConstraint}"/> </p:row> <p:row> <p:decimalbox constraint="no empty, no negative"/> </p:row> </p:rows> </p:grid> </p:window> </p:zk>
This code works as i expected, the constrained fired, when i put in nothing, a negative value or a value below 1000
Then i tried to put the zscript awai in a java-class. I thought, it was right here:
package ui; import org.zkoss.zk.ui.Page; import org.zkoss.zkplus.databind.AnnotateDataBinderInit; import org.zkoss.zul.SimpleConstraint; import ui.constraints.BidConstraint; public class Test4WindowInit extends AnnotateDataBinderInit { private BidConstraint myBidConstraint; @Override public void doAfterCompose(Page page) { myBidConstraint = new BidConstraint(SimpleConstraint.NO_EMPTY |SimpleConstraint.NO_NEGATIVE ,1000); page.setVariable("myBidConstraint", myBidConstraint); super.doAfterCompose(page); } }
The script i only expanded to use my AnnotateDataBinderInit :
Now no constrained seemed to be attached. With system.out.println i saw, that the constructor of BidConstrained was called. But no errormessages came up, if i enterd a wrong value in the input-field.
I don't undertand why it is not working. Is it the wrong place, to put it in doAfterCompose() and why?
Thanks in advice for helping me understand!
It is to later to assign a variable 'myBidConstraint' in afterCompose, because all children have created in parent's afterCompose.
Which means, in children's creation, the variable 'myBidConstraint' is not ready yet to been assigned.
Since you use init, I think you should override doInit(org.zkoss.zk.ui.Page page, java.lang.Object[] args) to assign a variable in JAVA Code.
/Dennis
Asked: 2008-08-31 12:42:14 +0800
Seen: 665 times
Last updated: Sep 01 '08