33 lines
962 B
Java
33 lines
962 B
Java
package eu.eudat.commons.validation.old;
|
|
|
|
import eu.eudat.commons.validation.old.FieldsValueMatch;
|
|
import org.springframework.beans.BeanWrapperImpl;
|
|
|
|
import jakarta.validation.ConstraintValidator;
|
|
import jakarta.validation.ConstraintValidatorContext;
|
|
|
|
public class FieldsValueMatchValidator implements ConstraintValidator<FieldsValueMatch, Object> {
|
|
|
|
private String field;
|
|
private String fieldMatch;
|
|
|
|
@Override
|
|
public void initialize(FieldsValueMatch constraintAnnotation) {
|
|
this.field = constraintAnnotation.field();
|
|
this.fieldMatch = constraintAnnotation.fieldMatch();
|
|
}
|
|
|
|
@Override
|
|
public boolean isValid(Object entity, ConstraintValidatorContext context) {
|
|
|
|
Object fieldValue = new BeanWrapperImpl(entity).getPropertyValue(field);
|
|
Object fieldMatchValue = new BeanWrapperImpl(entity).getPropertyValue(fieldMatch);
|
|
|
|
if (fieldValue != null) {
|
|
return fieldValue.equals(fieldMatchValue);
|
|
} else {
|
|
return fieldMatchValue == null;
|
|
}
|
|
}
|
|
}
|