ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

CutomConstraint - "catch" multiple errors

admin
26 Mar 2008 09:56:11 GMT
26 Mar 2008 09:56:11 GMT


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4864423

By: rene-w

I have a bunch of components (all textboxes) placed on my site which all have my customConstraint set as their constraint.
My goal is to "catch" all errors of them, highlight the textbox in red (which is default behauvior anyway), but instead of displaying the error-boxe for each control, to only display one error anywhere on the page.

Here is my custom constraint-code (is not fully ready, but is enough to do the
testing):
public class MyConstraint extends SimpleConstraint implements CustomConstraint{

/**
*
*/
private static final long serialVersionUID = 1L;
private Label errorOutput; // outputElement for custom Error message
private String errorMessage;

public MyConstraint(int constraintType, String errorMessage, Label errorOutput) {
super(constraintType);
this.errorMessage = errorMessage;
this.errorOutput = errorOutput;
}

public MyConstraint(String regexConstraint, String errorMessage, Label errorOutput) {
super(NO_EMPTY);
this.errorMessage = errorMessage;
this.errorOutput = errorOutput;
}

public void showCustomError(Component comp, WrongValueException ex) {
if (ex == null)
errorOutput.setValue("");
else
errorOutput.setValue(errorMessage);
}

public void validate(org.zkoss.zk.ui.Component comp, java.lang.Object value) {
super.validate(comp, value);
}
}

The validation of each control is started with sequentially calling
component.getValue() for each component placed on the site.
To make it easier I just have 2 textboxes placed with the constraint set. The weird thing is, if both controls are empty showCustomerror is called correctly (ex != null) but only for the first control thich was validated the background is set red, the second one is left untouched?
I changed the order of the .getValue()-calls, and now the former second is marked red, but other one isn't. So it is clear that the order in which they are called makes the difference.
And to make things worse, if I debug my code and have breakpoints set on
super.validate(...) and showCustomError I get it to work correctly, that both components are marked red!

Does anybody have a suggestion what the error could be?

I am sorry that I can't provide you with a full working sample, but I do work with complete java-code-behind classes (for windows on my page) and these are also integrated with other servlets....

thanks in advance, yours renew

admin
28 Mar 2008 07:18:57 GMT
28 Mar 2008 07:18:57 GMT


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4868781

By: waterbottle

Hi,
component.getValue() will trigger the validate and if fail to pass it will throw a WrongValueException.
so, if you validate by getValue() such as, comp1.getValue(); comp2.getValue(); When invoke comp1.getValue(), if fail to pass, then a WrongValueException will be thrown. and comp2.getValue() was not invoked.



Maybe you can change your code to :

public void validate(org.zkoss.zk.ui.Component comp, java.lang.Object value) {
try{
super.validate(comp, value);
}catch(WrongValueException x){
showCustomError(comp,x) {
throw x;
}
}

and when do batch validation..(write a generic Util class to do this with you
component)


boolean valid = true;
while(comps.hasNext(){
try{
comp = comps.next();
comp.getValue();
}catch(WrongValueException x){
valid = false;
}
}
return valid;



finally, you code will look like this,

void onBtnClick(){
if(!util.validate(comps)){
return;
}
//do save for comps
}


/Dennis