de.knightsoftnet.validators.shared.impl.NotEmptyIfOtherIsEmptyValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mt-bean-validators Show documentation
Show all versions of mt-bean-validators Show documentation
The MT Bean Validators is a collection of JSR-303/JSR-349/JSR-380 bean validators. It's extracted from
GWT Bean Validators and contains no dependencies to GWT.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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.knightsoftnet.validators.shared.impl;
import de.knightsoftnet.validators.shared.NotEmptyIfOtherIsEmpty;
import de.knightsoftnet.validators.shared.util.BeanPropertyReaderUtil;
import org.apache.commons.lang3.StringUtils;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
/**
* Check if a field is filled if another field is empty.
*
* @author Manfred Tremmel
*
*/
public class NotEmptyIfOtherIsEmptyValidator
implements ConstraintValidator {
/**
* error message key.
*/
private String message;
/**
* field name to check.
*/
private String fieldCheckName;
/**
* field name to compare.
*/
private String fieldCompareName;
/**
* {@inheritDoc} initialize the validator.
*
* @see jakarta.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
*/
@Override
public final void initialize(final NotEmptyIfOtherIsEmpty constraintAnnotation) {
message = constraintAnnotation.message();
fieldCheckName = constraintAnnotation.field();
fieldCompareName = constraintAnnotation.fieldCompare();
}
/**
* {@inheritDoc} check if given object is valid.
*
* @see jakarta.validation.ConstraintValidator#isValid(Object,
* jakarta.validation.ConstraintValidatorContext)
*/
@Override
public final boolean isValid(final Object value, final ConstraintValidatorContext context) {
if (value == null) {
return true;
}
try {
final String fieldCheckValue =
BeanPropertyReaderUtil.getNullSaveStringProperty(value, fieldCheckName);
final String fieldCompareValue =
BeanPropertyReaderUtil.getNullSaveStringProperty(value, fieldCompareName);
if (StringUtils.isEmpty(fieldCheckValue) && StringUtils.isEmpty(fieldCompareValue)) {
switchContext(context);
return false;
}
return true;
} catch (final Exception ignore) {
switchContext(context);
return false;
}
}
private void switchContext(final ConstraintValidatorContext context) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(message).addPropertyNode(fieldCheckName)
.addConstraintViolation();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy