de.swm.commons.mobile.client.validation.impl.ValidationHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swm-mobile Show documentation
Show all versions of swm-mobile Show documentation
GWT Bibliothek fuer Mobile Plattformen der SWM
/*
* Copyright 2011 SWM Services GmbH.
*
* 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 de.swm.commons.mobile.client.validation.impl;
import com.google.gwt.user.client.ui.HasValue;
import de.swm.commons.mobile.client.validation.IErrorOutputElement;
import de.swm.commons.mobile.client.validation.IValidator;
import java.util.ArrayList;
import java.util.List;
/**
* Helper class for validation.
* @author wiese.daniel
*
copyright (C) 2012, SWM Services GmbH
*
* @param the type of the field
*/
public class ValidationHelper {
private final List> validators = new ArrayList>();
private final List validatorErrorOutputs = new ArrayList();
/**
* Adds a validator to this Ui-Widget
*
* @param validatorToAdd
* the validtors to add
*/
public void addValidator(IValidator validatorToAdd) {
this.validators.add(validatorToAdd);
}
public void clearValidation() {
for (IErrorOutputElement error : validatorErrorOutputs) {
error.clearErrorMessage();
}
}
/**
* Add an error output element where error messages are displayed
*
* @param errorOutputToAdd
* the error output element to add
*/
public void addErrorOuptut(IErrorOutputElement errorOutputToAdd) {
this.validatorErrorOutputs.add(errorOutputToAdd);
}
/**
* Will remove all registered validators.
*/
public void removeAllValidators() {
this.validators.clear();
}
/**
* Executes the validator.
* @param owner the owning field
* @return true if the owning filed contains errors.
*/
public boolean validate(HasValue owner) {
boolean hasErrors = false;
if (!validators.isEmpty()) {
for (IValidator validator : validators) {
final String errorMessage = validator.validate(owner);
if (errorMessage != null) {
hasErrors = true;
for (IErrorOutputElement validatorErrorOutput : this.validatorErrorOutputs) {
validatorErrorOutput.appendErrorMessage(owner, errorMessage);
}
} else {
for (IErrorOutputElement validatorErrorOutput : this.validatorErrorOutputs) {
validatorErrorOutput.clearErrorMessage();
}
}
}
}
return hasErrors;
}
}