0

I have the following class that I'm trying to validate via Hibernate:

public class LoginForm
{
    @NotBlank(message = "{myCustomMessage}")
    private String user;

    @NotBlank(message = "{myCustomMessage}")
    private String pw;

   //....
}

This works fine and my custom message is correctly displayed for NotBlank, however if I have to specify the (message = "{myCustomMessage}") line for every single constraint, for every single member, in every single class, its going to become very repetitive.

Is there a way to globally set this message, so it doesn't have to be specified individually for every member? I.e so it will, by default, fetch myCustomMessage for @NotBlank? (And for all the other constraints that I specify it for).

Edit: So if I rename the message key from myCustomMessage to org.hibernate.validator.constraints.NotBlank.message , then I no longer need to specify the (message..) line. However, I'd still prefer if it could be made to work with myCustomMessage globally.

Ali
  • 261,656
  • 265
  • 575
  • 769
  • Please look at this previous answer: http://stackoverflow.com/questions/2235544/bean-validation-and-error-messages-at-properties-file – Romski Feb 19 '14 at 22:54
  • @Romski That doesn't answer my question – Ali Feb 19 '14 at 23:13
  • Sorry, just re-read your question and I see your issue. Other than the solution in your edit, I'm not aware of anything else. – Romski Feb 19 '14 at 23:19

1 Answers1

1

As you already found out, the only way would be to specify a value for the default key org.hibernate.validator.constraints.NotBlank.message. You also could define your own NotBlank constraint which specifies the value you want as default value for the message attribute, but I would not really recommend that.

Gunnar
  • 18,095
  • 1
  • 53
  • 73