Customize spring validation error

The JSR 303 default message interpolation algorithm allows you to customize messages by supplying a resource bundle named ValidationMessages. Create a ValidationMessages.properties file in the classpath containing:

javax.validation.constraints.NotNull.message=CUSTOM NOT NULL MESSAGE
javax.validation.constraints.Size.message=CUSTOM SIZE MESSAGE

This changes the default message for the @Size constraint, so you should use the @Size constraint instead of the Hibernate-specific @Length constraint.

Instead of changing the default message for all constraints, you can change the message for a specific constraint instance. Set the message attribute on the constraint:

@NotNull(message = "{email.notnull}")
private String email;

And add the message to the ValidationMessages.properties file:

email.notnull=E-mail address is required

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)