1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package nl.openedge.baritus.validation.impl;
32
33 import nl.openedge.baritus.FormBeanContext;
34 import nl.openedge.baritus.validation.AbstractFieldValidator;
35 import nl.openedge.baritus.validation.ValidationActivationRule;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.infohazard.maverick.flow.ControllerContext;
40
41 /***
42 * This validator checks on minimum length. If the type of the value is a String,
43 * the string length is checked. If the type of the value is a Number, the actual
44 * number is used. E.g. if property minLength is 4, "hello" will pass, but "hi"
45 * will fail, and number 5 will pass, but 2 will fail.
46 *
47 * @author Eelco Hillenius
48 */
49 public final class MinimumFieldLengthValidator extends AbstractFieldValidator
50 {
51 /*** special value that indicates there's no min value to check on */
52 public final static int NO_MINIMUM = -1;
53
54 private int minLength = NO_MINIMUM;
55
56 private static Log log = LogFactory.getLog(MinimumFieldLengthValidator.class);
57
58 private String errorMessageKey = "invalid.field.input.size";
59
60 /***
61 * Construct with key invalid.field.input.size for error messages.
62 */
63 public MinimumFieldLengthValidator()
64 {
65
66 }
67
68 /***
69 * Construct with message prefix for error message keys.
70 * @param errorMessageKey message key of error
71 */
72 public MinimumFieldLengthValidator(String errorMessageKey)
73 {
74 setErrorMessageKey(errorMessageKey);
75 }
76
77 /***
78 * Construct with message key for error message keys and set
79 * checking on minimum length with given length of fields only.
80 * @param errorMessageKey key for error messages
81 * @param minLength minimum length allowed for values; use -1 for no minimum
82 */
83 public MinimumFieldLengthValidator(String errorMessageKey, int minLength)
84 {
85 setErrorMessageKey(errorMessageKey);
86 setMinLength(minLength);
87 }
88
89 /***
90 * Construct with activation rule.
91 * @param rule activation rule
92 */
93 public MinimumFieldLengthValidator(ValidationActivationRule rule)
94 {
95 setValidationRule(rule);
96 }
97
98 /***
99 * Construct with errorMessageKey and activation rule.
100 * @param errorMessageKey
101 * @param rule activation rule
102 */
103 public MinimumFieldLengthValidator(
104 String errorMessageKey,
105 ValidationActivationRule rule)
106 {
107 setErrorMessageKey(errorMessageKey);
108 setValidationRule(rule);
109 }
110
111 /***
112 * Construct with min length.
113 * @param minLength minimum length allowed for values; use -1 for no minimum
114 */
115 public MinimumFieldLengthValidator(int minLength)
116 {
117 setMinLength(minLength);
118 }
119
120 /***
121 * Checks whether the provided value is greater than the minimum.
122 * In case the value is an instance of string: checks whether the length of the string
123 * is equal to or smaller than the minLength property.
124 * In case the value is an instance of number: checks whether the length of the integer
125 * value is equal to or smaller than the minLength property.
126 * @return boolean true if the length of value is equal to or less than the
127 * minLength property, false otherwise
128 * @see nl.openedge.baritus.validation.FieldValidator#isValid(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext, java.lang.String, java.lang.Object)
129 */
130 public boolean isValid(
131 ControllerContext cctx,
132 FormBeanContext formBeanContext,
133 String fieldName,
134 Object value)
135 {
136 boolean minExceeded = false;
137 if(value != null)
138 {
139 if(value instanceof String)
140 {
141 String toCheck = (String)value;
142 int length = toCheck.length();
143 if(minLength != NO_MINIMUM)
144 {
145 minExceeded = (length < minLength);
146 }
147 }
148 else if(value instanceof Number)
149 {
150 Number toCheck = (Number)value;
151 int length = toCheck.intValue();
152 if(minLength != NO_MINIMUM)
153 {
154 minExceeded = (length < minLength);
155 }
156 }
157 else
158 {
159
160 log.warn(fieldName + " with value: " + value +
161 " is of the wrong type for checking on length");
162 }
163 }
164
165 if(minExceeded)
166 {
167 setErrorMessage(formBeanContext, fieldName, getErrorMessageKey(),
168 new Object[]{getFieldName(formBeanContext, fieldName),
169 value, new Integer(minLength)});
170 }
171
172 return (!minExceeded);
173 }
174
175 /***
176 * @return int minimum length that is checked on
177 */
178 public int getMinLength()
179 {
180 return minLength;
181 }
182
183 /***
184 * @param i minimum length that is checked on
185 */
186 public void setMinLength(int i)
187 {
188 minLength = i;
189 }
190
191 /***
192 * Get key of error message.
193 * @return String key of error message
194 */
195 public String getErrorMessageKey()
196 {
197 return errorMessageKey;
198 }
199
200 /***
201 * Set key of error message.
202 * @param string key of error message
203 */
204 public void setErrorMessageKey(String string)
205 {
206 errorMessageKey = string;
207 }
208
209 }