Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate validiate list of integer

I have a list of integer like this:

private List<Integer> indexes;

Is there a way to valid individual member to be in a range of 0-9? I see @Range and @Valid but can't find a way to make it work with List.

Thanks for your help,

like image 457
Sean Nguyen Avatar asked Nov 30 '10 18:11

Sean Nguyen


People also ask

How do you check whether a list is empty or not?

The isEmpty() method of List interface in java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.

What is the difference between @valid and @validated?

The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.


1 Answers

Only @Size and @Valid can be used on Collections, however you can use some wrapper object instead of "Integer" to validate your ints, e.g.:

public class Index {
  @Range( min = 0, max = 9 )
  private Integer value;
}

public class Container {
  @Valid
  private List<Index> indexes;
}

This should do the trick

like image 129
Zilvinas Avatar answered Nov 15 '22 07:11

Zilvinas