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.

3
  • Please look at this previous answer: stackoverflow.com/questions/2235544/… Commented Feb 19, 2014 at 22:54
  • @Romski That doesn't answer my question Commented Feb 19, 2014 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. Commented Feb 19, 2014 at 23:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.