KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > util > ValidationUtil


1 package org.appfuse.webapp.util;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4
5 import org.apache.commons.validator.Field;
6 import org.apache.commons.validator.GenericValidator;
7 import org.apache.commons.validator.ValidatorAction;
8 import org.apache.commons.validator.util.ValidatorUtils;
9 import org.apache.struts.action.ActionMessages;
10 import org.apache.struts.validator.Resources;
11
12
13 /**
14  * ValidationUtil Helper class for performing custom validations that
15  * aren't already included in the core Struts Validator.
16  *
17  * <p>
18  * <a HREF="ValidationUtil.java.htm"><i>View Source</i></a>
19  * </p>
20  *
21  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
22  * @version $Revision: 1.4 $ $Date: 2004-05-15 20:18:10 -0600 (Sat, 15 May 2004) $
23  */

24 public class ValidationUtil {
25     //~ Methods ================================================================
26

27     /**
28      * Validates that two fields match.
29      * @param bean
30      * @param va
31      * @param field
32      * @param errors
33      * @param request
34      * @return boolean
35      */

36     public static boolean validateTwoFields(Object JavaDoc bean, ValidatorAction va,
37                                             Field field, ActionMessages errors,
38                                             HttpServletRequest JavaDoc request) {
39         String JavaDoc value =
40             ValidatorUtils.getValueAsString(bean, field.getProperty());
41         String JavaDoc sProperty2 = field.getVarValue("secondProperty");
42         String JavaDoc value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
43
44         if (!GenericValidator.isBlankOrNull(value)) {
45             try {
46                 if (!value.equals(value2)) {
47                     errors.add(field.getKey(),
48                                Resources.getActionMessage(request, va, field));
49
50                     return false;
51                 }
52             } catch (Exception JavaDoc e) {
53                 errors.add(field.getKey(),
54                            Resources.getActionMessage(request, va, field));
55
56                 return false;
57             }
58         }
59
60         return true;
61     }
62 }
63
Popular Tags