01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.markup.html.form.validation;
18:
19: import org.apache.wicket.markup.html.form.Form;
20: import org.apache.wicket.markup.html.form.FormComponent;
21: import org.apache.wicket.util.lang.Objects;
22:
23: /**
24: * Validates that the input of two form components is identical. Errors are
25: * reported on the second form component with key 'EqualInputValidator' and
26: * the variables:
27: * <ul>
28: * <li>${input(n)}: the user's input</li>
29: * <li>${name}: the name of the component</li>
30: * <li>${label(n)}: the label of the component - either comes from
31: * FormComponent.labelModel or resource key [form-id].[form-component-id] in
32: * that order</li>
33: * </ul>
34: *
35: * @author Igor Vaynberg (ivaynberg)
36: */
37: public class EqualInputValidator extends AbstractFormValidator {
38: /**
39: *
40: */
41: private static final long serialVersionUID = 1L;
42:
43: /** form components to be checked. */
44: private final FormComponent[] components;
45:
46: /**
47: * Construct.
48: *
49: * @param formComponent1
50: * a form component
51: * @param formComponent2
52: * a form component
53: */
54: public EqualInputValidator(FormComponent formComponent1,
55: FormComponent formComponent2) {
56: if (formComponent1 == null) {
57: throw new IllegalArgumentException(
58: "argument formComponent1 cannot be null");
59: }
60: if (formComponent2 == null) {
61: throw new IllegalArgumentException(
62: "argument formComponent2 cannot be null");
63: }
64: components = new FormComponent[] { formComponent1,
65: formComponent2 };
66: }
67:
68: /**
69: * @see org.apache.wicket.markup.html.form.validation.IFormValidator#getDependentFormComponents()
70: */
71: public FormComponent[] getDependentFormComponents() {
72: return components;
73: }
74:
75: /**
76: * @see org.apache.wicket.markup.html.form.validation.IFormValidator#validate(org.apache.wicket.markup.html.form.Form)
77: */
78: public void validate(Form form) {
79: // we have a choice to validate the type converted values or the raw
80: // input values, we validate the raw input
81: final FormComponent formComponent1 = components[0];
82: final FormComponent formComponent2 = components[1];
83:
84: if (!Objects.equal(formComponent1.getInput(), formComponent2
85: .getInput())) {
86: error(formComponent2);
87: }
88: }
89:
90: }
|