user2660234
user2660234

Reputation: 67

how to send arguments on messages.properties when JSR 303 validation

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

  1. how can I set the message dynamically by sending arguments as like "the number must bigger than {MIN_VALUE}"?
  2. how can I share the message? such like "Min.* = the number .... " is possible?

Upvotes: 2

Views: 3852

Answers (2)

John
John

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

Ralph
Ralph

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

Related Questions