0

Triggering multiple constraint validations at once

asked 2010-04-06 08:00:37 +0800

raevel2 gravatar image 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 flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2010-04-08 02:07:50 +0800

ashishd gravatar image ashishd flag of Taiwan
1972 6

Hi raevel2,
Please take a look at this similar thread.
Let us know if that doesn't solve your problem.
Thanks
- Ashish

link publish delete flag offensive edit

answered 2010-04-08 05:20:41 +0800

raevel2 gravatar image 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++) {
		errors.add(es<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);
}

link publish delete flag offensive edit

answered 2010-05-14 11:49:54 +0800

dorr gravatar image 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);
}

link publish delete flag offensive edit

answered 2010-05-17 02:45:19 +0800

ashishd gravatar image ashishd flag of Taiwan
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

link publish delete flag offensive edit

answered 2010-05-17 05:30:32 +0800

raevel2 gravatar image 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

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: 2010-04-06 08:00:37 +0800

Seen: 487 times

Last updated: May 17 '10

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