Reputation: 67
I have a class something like
class Sample{
@Min(1) @Max(20) private int num_seats;
...
}
and messages.properties
like
Min.sample.num_seats = the number must be bigger than 1
Question is
Upvotes: 2
Views: 3852
Reputation: 43
Thank you Ralph it has helped me a lot to find a solution. I would just add this:
I would use @Range in this case (except if you want two different message for min and max).
In Sample class
@Range(min = 1, max = 20)
private int num_seats;
And in messages.properties file
Range.sample.num_seats=The number must be between {2} and {1}.
Note that the min is argument numbered {2} and max is numbered {1} !
Upvotes: 3
Reputation: 120871
According to SPR-6730 (Juergen Hoellers comment) it should work in this way:
@Min(value="1", message="the number must be higher than {1}")
I have not tested it, but this is the way, I have understand the issue comment.
second question: You can share the text, be putting them in a message properties file. If you use the same key as the default does, then you override the default message. If you want not to override the default message, then you need an other key, and need to write the key in currly brackets in the message attribute.
message properties file
javax.validation.constraints.Min.message=My mew default message
someOtherKey=Some Other Message
Using the other key:
@Min(value="1", message="{someOtherKey}")
Upvotes: 1