JSR 303验证,如果一个字段等于“ something”,则这些其他字段不应为null

我正在使用JSR-303做一些自定义验证javax.validation。


我有一个领域。而且,如果在此字段中输入了某个值,则我希望不包含其他一些字段null。


我正在尝试解决这个问题。不确定我该如何称呼以帮助找到解释。


任何帮助,将不胜感激。我对此很陌生。


目前,我正在考虑自定义约束。但是我不确定如何从批注中测试相关字段的值。基本上我不确定如何从注释访问面板对象。


public class StatusValidator implements ConstraintValidator<NotNull, String> {


    @Override

    public void initialize(NotNull constraintAnnotation) {}


    @Override

    public boolean isValid(String value, ConstraintValidatorContext context) {

        if ("Canceled".equals(panel.status.getValue())) {

            if (value != null) {

                return true;

            }

        } else {

            return false;

        }

    }

}

这panel.status.getValue();给我带来麻烦..不确定如何完成此任务。


Helenr
浏览 660回答 3
3回答

波斯汪

在这种情况下,我建议编写自定义验证器,该验证器将在类级别进行验证(以允许我们访问对象的字段),只有在另一个字段具有特定值时才需要一个字段。请注意,您应该编写通用验证器,该验证器将获取2个字段名称,并且仅使用这2个字段。要要求多个字段,您应该为每个字段添加此验证器。使用以下代码作为想法(我尚未对其进行测试)。验证器界面/**&nbsp;* Validates that field {@code dependFieldName} is not null if&nbsp;* field {@code fieldName} has value {@code fieldValue}.&nbsp;**/@Target({TYPE, ANNOTATION_TYPE})@Retention(RUNTIME)@Constraint(validatedBy = NotNullIfAnotherFieldHasValueValidator.class)@Documentedpublic @interface NotNullIfAnotherFieldHasValue {&nbsp; &nbsp; String fieldName();&nbsp; &nbsp; String fieldValue();&nbsp; &nbsp; String dependFieldName();&nbsp; &nbsp; String message() default "{NotNullIfAnotherFieldHasValue.message}";&nbsp; &nbsp; Class<?>[] groups() default {};&nbsp; &nbsp; Class<? extends Payload>[] payload() default {};&nbsp; &nbsp; @Target({TYPE, ANNOTATION_TYPE})&nbsp; &nbsp; @Retention(RUNTIME)&nbsp; &nbsp; @Documented&nbsp; &nbsp; @interface List {&nbsp; &nbsp; &nbsp; &nbsp; NotNullIfAnotherFieldHasValue[] value();&nbsp; &nbsp; }}验证器实施/**&nbsp;* Implementation of {@link NotNullIfAnotherFieldHasValue} validator.&nbsp;**/public class NotNullIfAnotherFieldHasValueValidator&nbsp; &nbsp; implements ConstraintValidator<NotNullIfAnotherFieldHasValue, Object> {&nbsp; &nbsp; private String fieldName;&nbsp; &nbsp; private String expectedFieldValue;&nbsp; &nbsp; private String dependFieldName;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void initialize(NotNullIfAnotherFieldHasValue annotation) {&nbsp; &nbsp; &nbsp; &nbsp; fieldName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = annotation.fieldName();&nbsp; &nbsp; &nbsp; &nbsp; expectedFieldValue = annotation.fieldValue();&nbsp; &nbsp; &nbsp; &nbsp; dependFieldName&nbsp; &nbsp; = annotation.dependFieldName();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean isValid(Object value, ConstraintValidatorContext ctx) {&nbsp; &nbsp; &nbsp; &nbsp; if (value == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fieldValue&nbsp; &nbsp; &nbsp; &nbsp;= BeanUtils.getProperty(value, fieldName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String dependFieldValue = BeanUtils.getProperty(value, dependFieldName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (expectedFieldValue.equals(fieldValue) && dependFieldValue == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.disableDefaultConstraintViolation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.buildConstraintViolationWithTemplate(ctx.getDefaultConstraintMessageTemplate())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addNode(dependFieldName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addConstraintViolation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(ex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}验证器用法示例@NotNullIfAnotherFieldHasValue.List({&nbsp; &nbsp; @NotNullIfAnotherFieldHasValue(&nbsp; &nbsp; &nbsp; &nbsp; fieldName = "status",&nbsp; &nbsp; &nbsp; &nbsp; fieldValue = "Canceled",&nbsp; &nbsp; &nbsp; &nbsp; dependFieldName = "fieldOne"),&nbsp; &nbsp; @NotNullIfAnotherFieldHasValue(&nbsp; &nbsp; &nbsp; &nbsp; fieldName = "status",&nbsp; &nbsp; &nbsp; &nbsp; fieldValue = "Canceled",&nbsp; &nbsp; &nbsp; &nbsp; dependFieldName = "fieldTwo")})public class SampleBean {&nbsp; &nbsp; private String status;&nbsp; &nbsp; private String fieldOne;&nbsp; &nbsp; private String fieldTwo;&nbsp; &nbsp; // getters and setters omitted}注意,验证器实现使用库中的BeanUtils类,commons-beanutils但您也可以使用BeanWrapperImplSpring Framework中的类。

森林海

定义必须验证为true的方法并将@AssertTrue注释放在其顶部:&nbsp; @AssertTrue&nbsp; private boolean isOk() {&nbsp; &nbsp; return someField != something || otherField != null;&nbsp; }该方法必须以“ is”开头。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java