简体   繁体   English

javax验证-用于null检查的跨字段验证

[英]javax validation - cross field validation for null check

In the below code, I want to use javax validation to check if either both currency and amount is null or both are not null. 在下面的代码中,我想使用javax验证来检查货币和金额都为null还是都不为null。 The validation should throw an error if either one of those is not null and other is null. 如果其中一个不为null,另一个为null,则验证将引发错误。

Ex: currency == null && amount == null is true 例如:货币==空&&金额==空为true

currency == null && amount == 100 is false 货币==空&&金额== 100为假

currency == "Dollar" && amount == null is false 货币==“美元” &&金额== null为假

currency == "Dollar && amount == 100 is true 货币==“美元&&金额== 100为true

public class fee {
  private String currency;

  private Double amount;
}
public class TransactionFee {
  @NotNull
  private String basis;

  @NotNull
  private Double rate;
}

checks only if both are not null 仅检查两者都不为空

public class TransactionFee {
  @Null
  private String basis;

  @Null
  private Double rate;
}

checks only if both are null 仅检查是否均为空

I would like to know if there is any way to implement cross field validation for null check 我想知道是否有实现空检查的跨字段验证的任何方法

As @A Le Dref mentioned, you will need to use a class level validator instead of a field level validator. 正如@A Le Dref所提到的,您将需要使用类级别的验证器而不是字段级别的验证器。

An example can be found in this stack overflow answer . 可以在此堆栈溢出答案中找到一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM