32 lines
901 B
Java
32 lines
901 B
Java
|
package eu.eudat.commons.validation;
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|