Buğra Ekuklu
Buğra Ekuklu

Reputation: 3278

Configuring max and min parameters in @size validation annotation through properties

Normally, we would validate a model with some constraint like so:

public class PersonForm {
    @NotNull
    @Size(min=2, max=30)
    private String name;

    @NotNull
    @Min(18)
    private Integer age;
}

However, I would like to use configurable properties instead of constants in my implementation. For instance, let this the properties file:

personform.name.size.min=2
personform.name.size.max=30

and the form class...

public class PersonForm {
    @NotNull
    @Size(min="personform.name.size.min", max="personform.name.size.max")
    private String name;

    ...
}

Is this possible with a declarative, annotation-based approach? Thank you.

Upvotes: 2

Views: 1050

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40038

Try using Spring Expression language in annotation

 @Size(min="${personform.name.size.min}", max="${personform.name.size.max}")
private String name;

Upvotes: 2

Related Questions