0

Creating a custom constraint

asked 2009-11-04 09:43:52 +0800

Rico gravatar image Rico
175 1 1 2

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

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2009-11-06 07:30:39 +0800

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

updated 2009-11-06 07:53:55 +0800

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

link publish delete flag offensive edit

answered 2009-12-18 05:39:05 +0800

Rico gravatar image Rico
175 1 1 2

Thanks a bunch, it was the component in the WrongValueException that did the trick *bummer*

link publish delete flag offensive edit

answered 2009-12-21 08:40:55 +0800

Fooch gravatar image Fooch
48 2

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?

link publish delete flag offensive edit

answered 2009-12-22 01:16:49 +0800

Rico gravatar image Rico
175 1 1 2

updated 2009-12-22 01:17:53 +0800

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}" /> 


Maybe not as slick, but it works (make sure You have the complete package-name for Your CustomConstraint in the zscript, or that You import the package)...

(oh, and unless it is a cut'n'paste error, one quotation is missing in the textbox id in Your code snippet)

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: 2009-11-04 09:43:52 +0800

Seen: 6,523 times

Last updated: Dec 22 '09

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