1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package examples.validator;
20
21 import javax.servlet.http.HttpServletRequest;
22
23 import org.apache.commons.validator.Field;
24 import org.apache.commons.validator.GenericValidator;
25 import org.apache.commons.validator.ValidatorAction;
26 import org.apache.commons.validator.util.ValidatorUtils;
27 import org.apache.struts.action.ActionMessages;
28 import org.apache.struts.validator.Resources;
29
30 /***
31 * A custom validator example
32 *
33 * @version $Rev: 421486 $ $Date: 2006-07-12 20:37:08 -0700 (Wed, 12 Jul 2006) $
34 */
35 public class CustomValidator {
36
37
38
39 /***
40 * Constructor for CustomValidator.
41 */
42 public CustomValidator() {
43 super();
44 }
45
46
47
48 /***
49 * Example validator for comparing the equality of two fields
50 *
51 * http://struts.apache.org/userGuide/dev_validator.html
52 * http://www.raibledesigns.com/page/rd/20030226
53 */
54 public static boolean validateTwoFields(
55 Object bean,
56 ValidatorAction va,
57 Field field,
58 ActionMessages errors,
59 HttpServletRequest request) {
60
61 String value =
62 ValidatorUtils.getValueAsString(bean, field.getProperty());
63 String property2 = field.getVarValue("secondProperty");
64 String value2 = ValidatorUtils.getValueAsString(bean, property2);
65
66 if (!GenericValidator.isBlankOrNull(value)) {
67 try {
68 if (!value.equals(value2)) {
69 errors.add(
70 field.getKey(),
71 Resources.getActionMessage(request, va, field));
72
73 return false;
74 }
75 } catch (Exception e) {
76 errors.add(
77 field.getKey(),
78 Resources.getActionMessage(request, va, field));
79 return false;
80 }
81 }
82 return true;
83 }
84
85 }