01: /**
02: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or any later version.
08: *
09: * 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: */package olstore.validation;
27:
28: import java.io.Serializable;
29: import javax.servlet.http.HttpServletRequest;
30: import org.apache.commons.validator.Field;
31: import org.apache.commons.validator.GenericValidator;
32: import org.apache.commons.validator.ValidatorAction;
33: import org.apache.commons.validator.util.ValidatorUtils;
34: import org.apache.struts.action.ActionMessages;
35: import org.apache.struts.validator.Resources;
36:
37: public class Validator implements Serializable {
38:
39: /**
40: * This validateTwoFields method originates from http://struts.apache.org/userGuide/dev_validator.html
41: * and has been slightly alterted.
42: * <p>
43: * This method validates a text field only if it matches another text field and is not an empty string.
44: *
45: */
46: public static boolean validateTwoFields(Object bean,
47: ValidatorAction va, Field field, ActionMessages errors,
48: org.apache.commons.validator.Validator validator,
49: HttpServletRequest request) {
50:
51: String value = ValidatorUtils.getValueAsString(bean, field
52: .getProperty());
53: String sProperty2 = field.getVarValue("secondProperty");
54: String value2 = ValidatorUtils.getValueAsString(bean,
55: sProperty2);
56:
57: if (!GenericValidator.isBlankOrNull(value)) {
58: try {
59: if (!value.equals(value2)) {
60: errors.add(field.getKey(), Resources
61: .getActionError(request, va, field));
62:
63: return false;
64: }
65: } catch (Exception e) {
66: errors.add(field.getKey(), Resources.getActionError(
67: request, va, field));
68: return false;
69: }
70: }
71:
72: return true;
73: }
74:
75: }
|