I would like to know how to pass in the additional parameter as object x as below on isValid method. Anyone can provide help? object x will be passed in during runtime after getting the value from other object. i.e. ( i would like to pass a value retrieve from db to isValid method. )
Default isValid implementation as below
public boolean isValid(Object value, ConstraintValidatorContext ctx)
Expected isValid customization
public boolean isValid(Object value, ConstraintValidatorContext ctx, Object x)
Full source code as below
NotNullIfAnotherFieldHasValueValidator
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class NotNullIfAnotherFieldHasValueValidator
implements ConstraintValidator<NotNullIfAnotherFieldHasValue, Object> {
private static final Logger LOG = LogManager.getLogger(NotNullIfAnotherFieldHasValueValidator.class);
private String fieldName;
private String expectedFieldValue;
private String dependFieldName;
private String errorMessage;
@Override
public void initialize(NotNullIfAnotherFieldHasValue annotation) {
fieldName = annotation.fieldName();
expectedFieldValue = annotation.fieldValue();
dependFieldName = annotation.dependFieldName();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext ctx) {
if (value == null) {
return true;
}
return true;
}
}
NotNullIfAnotherFieldHasValue
@Target({TYPE, ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = NotNullIfAnotherFieldHasValueValidator.class)
@Documented
public @interface NotNullIfAnotherFieldHasValue {
String fieldName();
String fieldValue();
String dependFieldName();
String message() default "value is required";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({TYPE, ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface List {
NotNullIfAnotherFieldHasValue[] value();
}
}
SampleBean
@NotNullIfAnotherFieldHasValue.List({
@NotNullIfAnotherFieldHasValue(fieldName = "yesOrNo", fieldValue = "true", dependFieldName = "status") })
public class SampleBean {
private String status;
private String fieldOne;
private Boolean yesOrNo;
private Set<Byte> refOfficer;
public Set<Byte> getRefOfficer() {
return refOfficer;
}
public void setRefOfficer(Set<Byte> refOfficer) {
this.refOfficer = refOfficer;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getFieldOne() {
return fieldOne;
}
public void setFieldOne(String fieldOne) {
this.fieldOne = fieldOne;
}
public Boolean getYesOrNo() {
return yesOrNo;
}
public void setYesOrNo(Boolean yesOrNo) {
this.yesOrNo = yesOrNo;
}
}
Main Program
public static main (String args[])
{
SampleBean sampleBean = new SampleBean();
sampleBean.setYesOrNo(Boolean.True);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<VisitResource>> violations = validator.validate(sampleBean);
}