JSR303 validation on collection of common objects
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
精彩评论