01: package org.jreform.internal;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import org.jreform.MultiInput;
07:
08: /**
09: * Validates a multi-value input.
10: *
11: * @author armandino (at) gmail.com
12: */
13: class MultiInputValidator<T> {
14: private AbstractInputControl<T> input;
15: private List<T> values;
16: private boolean isValid;
17: private String errorMessage;
18:
19: @SuppressWarnings("unchecked")
20: MultiInputValidator(MultiInput<T> input) {
21: this .input = (AbstractInputControl<T>) input;
22: }
23:
24: ValidationResult<List<T>> validate(String[] valueAttributes) {
25: isValid = false;
26:
27: if (valueAttributes != null && valueAttributes.length > 0) {
28: isValid = isValidInput(valueAttributes);
29: } else {
30: if (!input.isRequired()) {
31: isValid = true;
32: } else {
33: errorMessage = "Invalid or missing value";
34: }
35: }
36:
37: return new ValidationResult<List<T>>(values, isValid,
38: errorMessage);
39: }
40:
41: private boolean isValidInput(String[] valueAttributes) {
42: boolean allValid = true;
43: List<T> parsedValues = new ArrayList<T>(valueAttributes.length);
44: ValueAttributeValidator<T> validator = new ValueAttributeValidator<T>(
45: input);
46:
47: // parse in all values even if they don't satisfy the criteria
48: for (String valueAttribute : valueAttributes) {
49: ValidationResult<T> result = validator
50: .validate(valueAttribute);
51:
52: if (result.getParsedValue() != null)
53: parsedValues.add(result.getParsedValue());
54:
55: if (!result.isValid()) {
56: allValid = false;
57: errorMessage = result.getErrorMessage();
58: break;
59: }
60: }
61:
62: values = parsedValues;
63:
64: return allValid;
65: }
66:
67: }
|