【问题标题】:How to validate a array using oval validation framework?如何使用椭圆验证框架验证数组?
【发布时间】:2012-07-27 07:13:44
【问题描述】:

我有一个 bean 对象并想使用椭圆验证框架来验证它的字段。 例如

public class Demo{

    @Range(min=1)
    private long id;

    @NotNull
    private long[] values;
}

如上,我有一个类型为 long 数组的字段。我想确保数组不为空,并且其中的每个元素都必须大于零。我不知道如何验证以确保其元素大于零。谁能告诉我?谢谢。

【问题讨论】:

    标签: java arrays validation oval


    【解决方案1】:

    你可以使用 applyTo 属性:

    @NotNull(appliesTo = {ConstraintTarget.CONTAINER, ConstraintTarget.VALUES})
    private long[] values;
    

    【讨论】:

      【解决方案2】:
      /* ConstraintTarget.CONTAINER(i.e List) and 
       * ConstraintTarget.VALUES( i.e List elements)
       * it means List shouldn't be null & Its elements shouldn't be null 
       */
      @NotNull(appliesTo= {ConstraintTarget.CONTAINER,ConstraintTarget.VALUES})
      /*
       * List size should be greater than or equal to 1
       */
      @Size(min=1)
      /*
       * ConstraintTarget.VALUES means List elements shouldn't be empty
       */
      @NotEmpty(appliesTo = {ConstraintTarget.VALUES})
      private List<String> roles;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-28
        • 1970-01-01
        相关资源
        最近更新 更多