View Javadoc

1   /*
2    * $Id: CustomValidator.java 421486 2006-07-13 03:37:08Z wsmoak $
3    *
4    * Copyright 2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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      // ------------------------------------------------------------ Constructors
38  
39      /***
40       * Constructor for CustomValidator.
41       */
42      public CustomValidator() {
43          super();
44      }
45  
46      // ---------------------------------------------------------- Public Methods
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  }