-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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.
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); }
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); }
Asked: 2010-04-06 08:00:37 +0800
Seen: 487 times
Last updated: May 17 '10