fr.ird.observe.validation.validators.MandatoryCollectionFieldValidation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-validation Show documentation
Show all versions of common-validation Show documentation
ObServe Toolkit Common Validation module
package fr.ird.observe.validation.validators;
/*-
* #%L
* ObServe Toolkit :: Common Validation
* %%
* Copyright (C) 2017 - 2020 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import com.opensymphony.xwork2.validator.ValidationException;
import org.nuiton.validator.xwork2.field.SkipableRequiredFieldValidator;
import java.util.Collection;
import static io.ultreia.java4all.i18n.I18n.n;
/**
* @author Tony Chemit - [email protected]
* @since 5
*/
public class MandatoryCollectionFieldValidation extends SkipableRequiredFieldValidator {
public MandatoryCollectionFieldValidation() {
setDefaultMessage(n("observe.validation.field.mandatory"));
}
@Override
public String getValidatorType() {
return "mandatoryCollection";
}
@Override
protected void validateWhenNotSkip(Object object) throws ValidationException {
String fieldName = getFieldName();
Object value = getFieldValue(fieldName, object);
if (value == null || ((Collection>) value).isEmpty()) {
addFieldError(fieldName, object);
}
}
}