(ConstraintValidator)如何将附加对象作为参数传递给isValid方法?

时间:2017-11-27 11:05:58

标签: java validation

我想知道如何在isValid方法中将附加参数作为对象x传递,如下所示。有谁可以提供帮助?在从其他对象获取值之后,将在运行时期间传入对象x。 ie(我想将从db检索的值传递给isValid方法。)

默认isValid实现如下

public boolean isValid(Object value,ConstraintValidatorContext ctx)

预期isValid定制

public boolean isValid(Object value,ConstraintValidatorContext ctx,Object x)

完整源代码如下

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;
    }

}

主程序

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);
}

0 个答案:

没有答案