# Triggering multiple constraint validations at once

raevel2
72 1 2

I have a form with a submit button, if the user presses the submit button I want all validations that I associate with the form fields to run.

My code is basically just constraint1.validate(comp1, val1); constraint2.validate(comp2, val2); etc

The problem I'm having is that when a validation fails, a WrongValueException is thrown and the following validations will not trigger (if constraint1 fails in this exampel, constraint2's validation will never run). I want all the errors to be displayed at once so the user can see everything that needs to be fixed.

How can I accomplish this? I want all the validations to run separately so that each related form control gets its own error dialog.

delete retag edit

## 5 Replies

ashishd
1972 6

Hi raevel2,
Let us know if that doesn't solve your problem.
Thanks
- Ashish

raevel2
72 1 2

Thank you Ashish! That was what I was looking for.

In case anyone is interested it seems like you cannot nest WrongValuesExceptions inside of each other, so you will need to iterate through and add to a new array to flatten the structure, and you also want to avoid unsafe cast warnings. Here's what I ended up doing:

List<WrongValueException> errors = new ArrayList<WrongValueException>();
try {
performSeveralValidations();
} catch (final WrongValuesException wves) {
WrongValueException[] es = wves.getWrongValueExceptions();
for (int i = 0; i < es.length; i++) {
}
}
// > Other validations here...
if (errors.size() > 0) {
WrongValueException[] es = new WrongValueException[errors.size()];
for (int i = 0; i < errors.size(); i++) {
es<i > = errors.get(i);
}
throw new WrongValuesException(es);
}


dorr
126 4

I have done what was recommended above, but only the last error is displaying. I see 2 being passed to WrongValuesException from debugging. What am I doing wrong?

if (errors.size() > 0)
{
WrongValueException[] es = new WrongValueException[errors.size()];
for (int i = 0; i < errors.size(); i++)
{
es<i > = errors.get(i);
}
throw new WrongValuesException(es);
}


ashishd
1972 6

Hi dorr,
it is difficult to guess what is going wrong there. Code seems ok to me. Did you try printing the actual errors list values?
What version of ZK are you using?

Thanks
- Ashish

raevel2
72 1 2

dorr: You seem to have copy-pasted the code from my post, since it contains the same syntax error :-)

Please paste the actual code you are using

[hide preview]