01: /*
02: * $Id: CustomValidator.java 471754 2006-11-06 14:55:09Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: package examples.validator;
23:
24: import javax.servlet.http.HttpServletRequest;
25:
26: import org.apache.commons.validator.Field;
27: import org.apache.commons.validator.GenericValidator;
28: import org.apache.commons.validator.ValidatorAction;
29: import org.apache.commons.validator.util.ValidatorUtils;
30: import org.apache.struts.action.ActionMessages;
31: import org.apache.struts.validator.Resources;
32:
33: /**
34: * A custom validator example
35: *
36: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
37: */
38: public class CustomValidator {
39:
40: // ------------------------------------------------------------ Constructors
41:
42: /**
43: * Constructor for CustomValidator.
44: */
45: public CustomValidator() {
46: super ();
47: }
48:
49: // ---------------------------------------------------------- Public Methods
50:
51: /**
52: * Example validator for comparing the equality of two fields
53: *
54: * http://struts.apache.org/userGuide/dev_validator.html
55: * http://www.raibledesigns.com/page/rd/20030226
56: */
57: public static boolean validateTwoFields(Object bean,
58: ValidatorAction va, Field field, ActionMessages errors,
59: HttpServletRequest request) {
60:
61: String value = ValidatorUtils.getValueAsString(bean, field
62: .getProperty());
63: String property2 = field.getVarValue("secondProperty");
64: String value2 = ValidatorUtils
65: .getValueAsString(bean, property2);
66:
67: if (!GenericValidator.isBlankOrNull(value)) {
68: try {
69: if (!value.equals(value2)) {
70: errors.add(field.getKey(), Resources
71: .getActionMessage(request, va, field));
72:
73: return false;
74: }
75: } catch (Exception e) {
76: errors.add(field.getKey(), Resources.getActionMessage(
77: request, va, field));
78: return false;
79: }
80: }
81: return true;
82: }
83:
84: }
|