开发者

JSR303 validation on collection of common objects

开发者 https://www.devze.com 2023-03-30 08:50 出处:网络
Is it possible to validate each element of a collection, based one or more deleg开发者_StackOverflow中文版ate validation rules? For example:

Is it possible to validate each element of a collection, based one or more deleg开发者_StackOverflow中文版ate validation rules? For example:

@EachElement({@Min(1), @Max(12)})
private Set<Integer> monthNumbers;


Take a look at validator-collection, with this library is very easy to use any Constraint Annotation on a collection of simple types.

@EachMin(1)
@EachMax(12)
private Set<Integer> monthNumbers;

Also see https://stackoverflow.com/a/16023061/2217862.


Have a look at this answer: Hibernate Validation of Collections of Primitives. That describes a solution which work for you but it is pretty complex. A simpler solution might be to implement a wrapper class for your Integer and declare @Min and @Max in that class. Than you can use

@Valid
private Set<MyIntegerWrapper> monthNumbers;

class MyIntegerWrapper:

class MyIntegerWrapper
{
   @Min(1)
   @Max(12)
   Integer myInteger;
}

Here you find some documentation for @Valid: Object graphs

0

精彩评论

暂无评论...
验证码 换一张
取 消