# Forcing field validations

raevel2
72 1 2

I have a few form fields that i use constraints to validate. These fields depend on other fields in the form, so onChange is not the only time that i want validations to occur.

How do I force a validation to occur? I also want to make sure that no old validation errors remain on the page.

delete retag edit

## 4 Replies

9393 3 7 16
http://www.oxitec.de/

you can force a validation with getting the value for the field i.e.:
textbox.getValue();

best
Stephan

raevel2
72 1 2

Are you sure?

I'm calling getValue on my textbox but the constraint is not being called, and the error message remains. If I focus and blur the field manually, the validation triggers again.

This is a wild guess: When I call getValue the validation will only trigger if the value has changed since the last validation.

Here's an example, if you click the fail button immediately on page load, the validation error will trigger (since a validation has not yet occured?). After that the only way to get the validation trigger is to blur the field:

<?page contentType="text/html;charset=UTF-8"?>
<zk>
<zscript><![CDATA[
import org.zkoss.zul.Constraint;
import org.zkoss.zk.ui.WrongValueException;
boolean fail = false;
Constraint con = new Constraint() {
public void validate(Component comp, Object val) throws WrongValueException {
if (fail) {
throw new WrongValueException(comp, "fail");
}
}
}
]]></zscript>
<window>
<button id="button_fail" onClick="fail = true; textbox.getValue()" label="Fail"/>
<button id="button_succeed" onClick="fail = false; textbox.getValue()" label="Succeed"/>
<textbox id="textbox" constraint="\${con}"/>
</window>
</zk>


9393 3 7 16
http://www.oxitec.de/

Yes, i'm sure.

I have tested your code with 3.6.3 and 5.0.1. Seems in 5.0.1 it's more angry than in 3.6.3.
But you must focus the textbox after an error appears. The errormessage will show you which component must be corrected.
Yes, you are right that does not react correctly.

I think it's a problem of BSH. If you do it in a java controller without (zscript and bsh) it works fine.
Hope the ZK team can have a look on it.

best
Stephan

henrichen
3869 2

Hi Stephan,

I don't quite get what the issue is. Would you help post to ZK bugs that we can track it. Thanks.

[hide preview]