View Javadoc

1   package org.appfuse.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.springmodules.validation.commons.FieldChecks;
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       */
31      public static boolean validateTwoFields(Object bean, ValidatorAction va,
32                                              Field field, Errors errors) {
33          String value =
34              ValidatorUtils.getValueAsString(bean, field.getProperty());
35          String sProperty2 = field.getVarValue("secondProperty");
36          String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
37  
38          if (!GenericValidator.isBlankOrNull(value)) {
39              try {
40                  if (!value.equals(value2)) {
41                      FieldChecks.rejectValue(errors, field, va);
42                      return false;
43                  }
44              } catch (Exception e) {
45                  FieldChecks.rejectValue(errors, field, va);
46                  return false;
47              }
48          }
49  
50          return true;
51      }
52  }