0
votes

By default org.hibernate.validator.constraints.Email shows not a well-formed email address but I want to show foo is not a well-formed email address if the user enters just foo where an well formatted email is required. So I need to have a placeholder for foo. The property in my domain object is annotated with @Email.

I have Email = {0} is not a well-formed email address in my resource bundle file which shows email is not a well-formed email address when the user enters foo for the email field.

Is it possible to generate that entered value and display it for the error message?

I am using Hibernate 3.5 with Spring MVC 3.

Thanks.

1

1 Answers

1
votes

If you're working with Hibernate Validator as your Bean Validation Provider, you can use the ValueFormatterMessageInterpolator introduced in release 4.2. To do so just set that interpolator when retrieving your Validator, e.g. like this:

Configuration<?> configuration = Validation.byDefaultProvider().configure();

Validator validator = configuration
    .messageInterpolator(new ValueFormatterMessageInterpolator(
        configuration.getDefaultMessageInterpolator()))
    .buildValidatorFactory()
    .getValidator();

Using that interpolator you can refer to the validated value by the placeholder {validatedValue} from within the error message: {validatedValue} is not a well-formed email address.

You can find out more in the HV reference guide.