KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > validation > Validator


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27 package olstore.validation;
28
29
30 import java.io.Serializable;
31 import javax.servlet.http.HttpServletRequest;
32 import org.apache.commons.validator.Field;
33 import org.apache.commons.validator.GenericValidator;
34 import org.apache.commons.validator.ValidatorAction;
35 import org.apache.commons.validator.ValidatorUtil;
36 import org.apache.struts.action.ActionMessages;
37 import org.apache.struts.validator.Resources;
38
39 public class Validator implements Serializable {
40
41
42     /**
43      * This validateTwoFields method originates from http://struts.apache.org/userGuide/dev_validator.html
44      * and has been slightly alterted.
45      * <p>
46      * This method validates a text field only if it matches another text field and is not an empty string.
47      *
48      */

49     public static boolean validateTwoFields(
50             Object bean,
51             ValidatorAction va,
52             Field field,
53             ActionMessages errors,
54             org.apache.commons.validator.Validator validator,
55             HttpServletRequest request) {
56
57         String value = ValidatorUtil.getValueAsString(
58                 bean,
59                 field.getProperty());
60         String sProperty2 = field.getVarValue("secondProperty");
61         String value2 = ValidatorUtil.getValueAsString(
62                 bean,
63                 sProperty2);
64
65         if (!GenericValidator.isBlankOrNull(value)) {
66             try {
67                 if (!value.equals(value2)) {
68                     errors.add(field.getKey(),
69                             Resources.getActionError(request,va,field));
70
71                     return false;
72                 }
73             } catch (Exception e) {
74                 errors.add(field.getKey(),
75                         Resources.getActionError(request, va,field));
76                 return false;
77             }
78         }
79
80         return true;
81     }
82
83 }
84
Popular Tags