0

trouble with custom constraint

asked 2008-08-31 12:42:14 +0800

beginner gravatar image beginner
123 2

updated 2008-08-31 12:46:25 +0800

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 :



<?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" ?>
<?init class="ui.Test4WindowInit" ?>
<p:window id="bid"
title="Test Constraints"
border="normal"
width="300px"
sizable="true"
closable="true">
<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>

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!

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2008-09-01 00:53:49 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

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

link publish delete flag offensive edit

answered 2008-09-01 08:11:03 +0800

beginner gravatar image beginner
123 2

Thanks Dennis,

i assigned myBidConstraint in doInit, and now it works. Thanks a lot!

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: 2008-08-31 12:42:14 +0800

Seen: 665 times

Last updated: Sep 01 '08

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