4

i am working on a JSF Projekt with Glassfish. My validation works well but i dont become a custom error message.

//Class = User, package = devteam
@NotEmpty @Pattern(".+@.+\\.[a-z]+")
private String emailAddress;

My ValidationMessages.properties is in the WEB-INF folder with this content:

devteam.User.emailAddress=Invalid e-mail address

Thank you.

ThreeFingerMark
  • 989
  • 3
  • 12
  • 21

2 Answers2

6

You are having two problems here. First, the location of the ValidationMessages.properties file. It has to be in the root of the classpath, so move it into WEB-INF/classes Your second problems are the message keys. The default message key for the Pattern constraint for example is {javax.validation.constraints.Pattern.message}. In your case you want to specify the message parameter in the @Pattern annotation:

@Pattern(regexp=".+@.+\\.[a-z]+", message="{devteam.User.emailAddress}")
Mike Partridge
  • 5,128
  • 8
  • 35
  • 47
Hardy
  • 18,659
  • 3
  • 49
  • 65
  • I'm not using tomcat, so I can't add the properties file to WEB-INF/classes. Instead I'm doing a scala project in eclipse, but I can't get it to use the properties file. Where would you add the properties file in a standard eclipse project? – Marius Apr 01 '13 at 17:41
  • @Marius, the archive root directory – Hardy Apr 02 '13 at 08:34
  • @Hardy I've created another question about it: http://stackoverflow.com/questions/15748884/bval-validationmessages-properties-in-scala-eclipse-project – Marius Apr 02 '13 at 09:29
1

You should put the file in the root, then

devteam.User.emailAddress[Pattern] = "Your message here"

notice [Pattern] to specify the message to output when the Pattern has a constraint violation. this makes it easier to maintain in my opinion vs having the messages like

@Patterh(regexp ="xx", message = "your message here")

for every setter

EdChum
  • 376,765
  • 198
  • 813
  • 562