View Javadoc

1   package com.panogenesis.webapp.util;
2   
3   import org.apache.commons.validator.Field;
4   import org.apache.commons.validator.GenericValidator;
5   import org.apache.commons.validator.ValidatorAction;
6   import org.apache.commons.validator.util.ValidatorUtils;
7   import org.springframework.validation.Errors;
8   import org.springframework.validation.commons.Resources;
9   
10  
11  /***
12   * ValidationUtil Helper class for performing custom validations that
13   * aren't already included in the core Commons Validator.
14   *
15   * <p>
16   * <a href="ValidationUtil.java.html"><i>View Source</i></a>
17   * </p>
18   *
19   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
20   */
21  public class ValidationUtil {
22      //~ Methods ================================================================
23  
24      /***
25       * Validates that two fields match.
26       * @param bean
27       * @param va
28       * @param field
29       * @param errors
30       * @param request
31       * @return boolean
32       */
33      public static boolean validateTwoFields(Object bean, ValidatorAction va,
34                                              Field field, Errors errors) {
35          String value =
36              ValidatorUtils.getValueAsString(bean, field.getProperty());
37          String sProperty2 = field.getVarValue("secondProperty");
38          String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
39  
40          if (!GenericValidator.isBlankOrNull(value)) {
41              try {
42                  if (!value.equals(value2)) {
43                      Resources.rejectValue(errors, field, va);
44                      return false;
45                  }
46              } catch (Exception e) {
47                  Resources.rejectValue(errors, field, va);
48                  return false;
49              }
50          }
51  
52          return true;
53      }
54  }