-
-
Save XakepSDK/391783e3b79f839a93a8f45c886b38e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dk.xakeps.truestarter.server.validator; | |
import org.apache.commons.beanutils.PropertyUtils; | |
import javax.validation.ConstraintValidator; | |
import javax.validation.ConstraintValidatorContext; | |
import javax.validation.ValidationException; | |
import java.lang.reflect.InvocationTargetException; | |
public class CrossFieldValidator implements ConstraintValidator<CrossField, Object> { | |
private String[] fields; | |
@Override | |
public void initialize(CrossField constraintAnnotation) { | |
this.fields = constraintAnnotation.fields(); | |
} | |
@Override | |
public boolean isValid(Object value, ConstraintValidatorContext context) { | |
Object prevFieldValue = null; | |
int i = 0; | |
for (String field : fields) { | |
try { | |
Object property = PropertyUtils.getProperty(value, field); | |
if (i == 0) { | |
prevFieldValue = property; | |
i++; | |
continue; | |
} | |
if (!prevFieldValue.equals(property)) { | |
return false; | |
} | |
prevFieldValue = property; | |
i++; | |
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | |
throw new ValidationException("Failed validating bean", e); | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment