/** * Copyright 2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.bricket.plugin.contact.web; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.FormComponent; import org.apache.wicket.markup.html.form.validation.AbstractFormValidator; /** * @author Ingo Renner * @author Henning Teek * */ public class OptionalInputValidator extends AbstractFormValidator { private final FormComponent<String>[] components; /** * @param components * to be checked. */ public OptionalInputValidator(FormComponent<String>... components) { if (components == null) { throw new IllegalArgumentException("argument FormComponent[] components cannot be null"); } this.components = components.clone(); } @Override public FormComponent<String>[] getDependentFormComponents() { return components.clone(); } @Override public void validate(Form<?> form) { // we have a choice to validate the type converted values or the raw // input values, we validate the raw input boolean empty = true; for (FormComponent<String> component : components) { empty = empty && isEmpty(component.getInput()); } if (empty) { for (FormComponent<String> component : components) { error(component); } } } private boolean isEmpty(String s) { if (s == null || "".equals(s)) { return true; } return false; } }