32

I want customize the spring validation error for

    @NotNull
    @Length(max = 80)
    private String email; 

but I'm unable to do it. What are the step to follow?

Kara
  • 6,115
  • 16
  • 50
  • 57
Roberto de Santis
  • 868
  • 5
  • 15
  • 26

2 Answers2

71

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
Chin Huang
  • 12,912
  • 4
  • 46
  • 47
  • 14
    I think a worthy sidebar to add here is that you can get the min and max values of the size like `javax.validation.constraints.Size.message=Length must be between {min} and {max} characters` – Patrick Dec 18 '12 at 23:10
  • 5
    how do i do internacionalization with this properties file? ValidationMessages_.properties ? – Gonçalo Sep 01 '15 at 18:53
  • @Chin Huang, What if the values of {min/max] may change dynamically? Can one specify as message parameter {The minimum value is {provided_at_runtime}}, where provided_at_runtime does not referencing an annotation attribute – Werner Erasmus Feb 10 '17 at 08:34
  • How do I change the color to red? – Martin Erlic Feb 16 '17 at 14:54
  • 1
    @Gonçalo, Yes, That seems to be the way. Check https://docs.oracle.com/javaee/6/tutorial/doc/gkahi.html – Eagle_Eye Feb 18 '18 at 06:10
  • @santafebound, Spring tag has a cssClass property to customize styles. – Eagle_Eye Feb 18 '18 at 06:12
0

By Spring I am assuming you mean Spring MVC.

From the below reference http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

Here you go -

You create a validator class -

public class UserValidator implements Validator {

    public boolean supports(Class candidate) {
        return User.class.isAssignableFrom(candidate);
    }

    public void validate(Object obj, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "required", "Field is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "required", "Field is required.");
    }
}

Put in any validation text you want in the above field.

In the JSP you will need the following tag -

<tr>
    <td>First Name:</td>
    <td><form:input path="firstName" /></td>
    <!-- Show errors for firstName field -->
    <td><form:errors path="firstName" /></td>
</tr>

This way any validation error for firstName will be printed.

bluish
  • 26,356
  • 27
  • 122
  • 180
Pushkar
  • 7,450
  • 10
  • 38
  • 57