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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top