1 /*
2 * $Id$
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package examples.validator;
23
24 import jakarta.servlet.http.HttpServletRequest;
25
26 import org.apache.commons.validator.Field;
27 import org.apache.commons.validator.GenericValidator;
28 import org.apache.commons.validator.Validator;
29 import org.apache.commons.validator.ValidatorAction;
30 import org.apache.commons.validator.util.ValidatorUtils;
31 import org.apache.struts.action.ActionMessages;
32 import org.apache.struts.validator.Resources;
33
34 /**
35 * A custom validator example
36 *
37 * @version $Rev$ $Date$
38 */
39 public class CustomValidator {
40
41 // ------------------------------------------------------------ Constructors
42
43 /**
44 * Constructor for CustomValidator.
45 */
46 public CustomValidator() {
47 super();
48 }
49
50 // ---------------------------------------------------------- Public Methods
51
52 /**
53 * Example validator for comparing the equality of two fields
54 *
55 * <ul>
56 * <li><a href="https://weblegacy.github.io/struts1/faqs/validator.html">
57 * https://weblegacy.github.io/struts1/faqs/validator.html
58 * </a>
59 * </li>
60 * <li><a href="https://www.raibledesigns.com/rd/date/20030226">
61 * https://www.raibledesigns.com/rd/date/20030226
62 * </a>
63 * </li>
64 * </ul>
65 */
66 public static boolean validateTwoFields(
67 Object bean,
68 ValidatorAction va,
69 Field field,
70 ActionMessages errors,
71 Validator validator,
72 HttpServletRequest request) {
73
74 String value =
75 ValidatorUtils.getValueAsString(bean, field.getProperty());
76 String property2 = field.getVarValue("secondProperty");
77 String value2 = ValidatorUtils.getValueAsString(bean, property2);
78
79 if (!GenericValidator.isBlankOrNull(value)) {
80 try {
81 if (!value.equals(value2)) {
82 errors.add(
83 field.getKey(),
84 Resources.getActionMessage(validator, request, va, field));
85
86 return false;
87 }
88 } catch (Exception e) {
89 errors.add(
90 field.getKey(),
91 Resources.getActionMessage(validator, request, va, field));
92 return false;
93 }
94 }
95 return true;
96 }
97
98 }