Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting dynamic data to min and max attributes of @Range annotation - hibernate validators

Iam using Hibernate Validator for validation of data. I have used @Range attribute for validating a particular field.

@Range(min=0,max=100)
private String amount;

This is fine but can i dynamically change the values of min and max instead of hard coding. I mean can i do something like:

@Range(min=${},max=${})
private String amount;
like image 204
Swapna Ch Avatar asked Jun 26 '15 09:06

Swapna Ch


1 Answers

Annotations in Java uses constants as parameters. You Cannot change them dynamically.

Compile constants can only be primitives and Strings.Check this link.

IF you want to make it configurable you can declare them as static final.

For Example:

private static final int MIN_RANGE = 1;

private static final int MAX_RANGE = 100;

and then assign in annotation.

@Range(min=MIN_RANGE,max=MAX_RANGE)
private String amount;

The value for annotation attribute must be a constant expression.

like image 119
Deepak Kumar Avatar answered Oct 20 '22 05:10

Deepak Kumar