0

I'm using @Pattern to validate a field:

@Pattern(regexp = Patterns.ZIP_CODE, message="validation.ZIP_CODE")
private String zip;

In my messages.properties file I have this:

validation.ZIP_CODE=Must match NNNNN or NNNNN-NNNN

The javadocs seem to imply that it should look up the validation.ZIP_CODE message:

Returns:
The error message template.
Default:
"{javax.validation.constraints.Pattern.message}"

But instead I get the text:

validation.ZIP_CODE

Am I misunderstanding the javadoc, or am I implementing this incorrectly?

user1071914
  • 3,295
  • 11
  • 50
  • 76

1 Answers1

5

In order to use an internationalized message in a JEE 6 Pattern annotation, I believe you must wrap the property name in open/close curly braces.

@Pattern(regexp = Patterns.ZIP_CODE, message="{validation.ZIP_CODE}")
private String zip;
g8torPaul
  • 1,193
  • 1
  • 7
  • 12
  • No success here - I have tried "{validation.ZIP_CODE}" and "{messages.validation.ZIP_CODE}" and what I get back is the same value wrapped in the curly braces. – user1071914 Dec 10 '13 at 16:48
  • Ha. Quite by accident I stumbled on the answer (well, AN answer anyway.) The JEE6 documentation here: http://docs.oracle.com/javaee/6/tutorial/doc/gkahi.html indicates that "The Validationmessages resource bundle and the locale variants of this resource bundle contain strings that override the default validation messages. " So if you want your messages to appear, you have to have {curly.braces} around them and they have to go in the ValidationMessages.properties file (or files.) – user1071914 Dec 10 '13 at 17:00
  • I don't understand this explanation. Please provide a sample. or better still correct my error. Thank you. my properties file has the following zone.error = Zone code must be two alphabet characters in length. my @Pattern message="{zone.error}" – jimmy Oct 12 '19 at 02:39
  • What about if i want to read regexp from a properties file. How can I achieve this? – Ajay Feb 12 '21 at 09:40