如何从类级别注释违规中获取属性路径

发布于 2024-11-14 14:20:13 字数 354 浏览 3 评论 0原文

我正在使用 Hibernate 验证器。我有一个类级别的注释。它比较三个属性是否相等。执行验证时,我需要从返回的 javax.validation.ConstraintViolation 对象中获取 PropertyPaths。由于它不是单个字段,因此 getPropertyPath() 方法返回 null。还有其他方法可以找到 PropertyPaths吗?

这是我的注释实现 -

@MatchField.List({
@MatchField(firstField = "firstAnswer", secondField = "secondAnswer", thirdField = "thirdAnswer"),
})

I'm using Hibernate Validator. I have a Class level annotation. It compares three properties for equality. When the validation is performed I need to get the PropertyPaths from the javax.validation.ConstraintViolation object returned. Since it's not a single field the getPropertyPath() method returns null. Is there another way to find the PropertyPaths?

This is my annotation implementation -

@MatchField.List({
@MatchField(firstField = "firstAnswer", secondField = "secondAnswer", thirdField = "thirdAnswer"),
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念﹏祤嫣 2024-11-21 14:20:13

您需要将消息设置为映射到进行验证时要拒绝的属性。 Hibernate Validator 无法自动找出自定义注释属性是属性路径。

public class MatchFieldValidator implements ConstraintValidator<MatchField, Object> {

  private MatchField matchField;

  @Override
  public void initialize(MatchField matchField) {
    this.matchField = matchField;
  }

  @Override
  public boolean isValid(Object obj, ConstraintValidatorContext cvc) {

    //do whatever you do
    if (validationFails) {
      cvc.buildConstraintViolationWithTemplate("YOUR FIRST ANSWER INPUT IS WRONG!!!").
                        addNode(matchField.firstAnswer()).addConstraintViolation();
      cvc.buildConstraintViolationWithTemplate("YOUR SECOND ANSWER INPUT IS WRONG!!!").
                        addNode(matchField.secondAnswer()).addConstraintViolation();
      //you get the idea
      cvc.disableDefaultConstraintViolation();
      return false;
    }
  }
}

You need to set the messages to map to the properties that you want rejected when you do the validation. Hibernate Validator has no way to auto-magically figure out that custom annotation properties are property paths.

public class MatchFieldValidator implements ConstraintValidator<MatchField, Object> {

  private MatchField matchField;

  @Override
  public void initialize(MatchField matchField) {
    this.matchField = matchField;
  }

  @Override
  public boolean isValid(Object obj, ConstraintValidatorContext cvc) {

    //do whatever you do
    if (validationFails) {
      cvc.buildConstraintViolationWithTemplate("YOUR FIRST ANSWER INPUT IS WRONG!!!").
                        addNode(matchField.firstAnswer()).addConstraintViolation();
      cvc.buildConstraintViolationWithTemplate("YOUR SECOND ANSWER INPUT IS WRONG!!!").
                        addNode(matchField.secondAnswer()).addConstraintViolation();
      //you get the idea
      cvc.disableDefaultConstraintViolation();
      return false;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文