Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring MVC do not resolve correctly Custom Validation messages using Hibernate Validator [SPR-8658] #13300

Closed
spring-projects-issues opened this issue Aug 31, 2011 · 8 comments
Assignees
Labels
in: web

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 31, 2011

Angel opened SPR-8658 and commented

I have the following Spring beans definition file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="messageSources" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:i18n/messages</value>
                <value>classpath:i18n/validation</value>
            </list>
        </property>
    </bean>

    <!-- JSR-303 -->
    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSources"/>
     </bean>
</beans>

the following properties:

NotEmpty.custom=is required.
registerForm.register.abn=ABN
NotEmpty.abnYn={registerForm.register.abn} {NotEmpty.custom}

and this POJO:

public class Registration implements Serializable {

    @NotEmpty(message = "{NotEmpty.abnYn}")
    private String abnYn;
    private String abn;

    public String getAbnYn() {
        return abnYn;
    }

    public void setAbnYn(String abnYn) {
        this.abnYn = abnYn;
    }

    public String getAbn() {
        return abn;
    }

    public void setAbn(String abn) {
        this.abn = abn;
    }
}

If I execute this test:

public class BeanValidationTest {

    private static ApplicationContext applicationContext;

    @BeforeClass
    public static void initialize() {
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/resource-bundle.xml");
        assertNotNull(applicationContext);
    }

    @Test
    public void test() {
        LocalValidatorFactoryBean factory = applicationContext.getBean("validator", LocalValidatorFactoryBean.class);
        Validator validator = factory.getValidator();
        Registration registration = new Registration();
        Set<ConstraintViolation<Registration>> violations = validator.validate(registration);
        for(ConstraintViolation<Registration> violation : violations) {
            System.out.println(violation.getMessage());
        }
    }
}

After executing the test I get the following result:

abn is required.

But if I try to do same within a form using Spring MVC the resolved message is just this:

{registerForm.register.abn} {NotEmpty.custom}

because the message interpolator is not resolving the brackets recursively as it is supposed to do:
http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html/validator-usingvalidator.html#section-message-interpolation

The interpolator will try to resolve any message parameters, meaning string literals enclosed in braces. In order to resolve these parameters Hibernate Validator's default MessageInterpolator first recursively resolves parameters against a custom ResourceBundle called ValidationMessages.properties at the root of the classpath (It is up to you to create this file). If no further replacements are possible against the custom bundle the default ResourceBundle under /org/hibernate/validator/ValidationMessages.properties gets evaluated.

From the test above it is clear what the behaviour should be.


Affects: 3.1 M2

Referenced from: commits spring-attic/spring-framework-issues@d76b671

8 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 11, 2011

Stephen Morrison commented

Are there any workarounds for this? I am also able to recreate this issue.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 11, 2011

Rossen Stoyanchev commented

I've created a project for the issue in the Spring Framework Issues repository but I can't seem to be able to reproduce it.

Feel free to fork, modify, and submit a pull request:

https://github.com/SpringSource/spring-framework-issues/tree/master/SPR-8658

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Dec 1, 2011

Rossen Stoyanchev commented

Anyone who experienced the issue, please send a pull request for this to have any chance of being included in Spring 3.1 GA.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 17, 2012

Eduardo Narros commented

I am able to reproduce this in 3.1.0.RELEASE.

Also if I plug my own interpolator into the LocalValidatorFactoryBean, it does not get called and instead the hibernate defaults continue being used.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 17, 2012

Rossen Stoyanchev commented

Would you be able to demonstrate it? See comments above and link to Issues repository.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 21, 2012

Eduardo Narros commented

Hi Rossen, thanks for you quick reply, I was missing the setting of the validator attribute at:

<mvc:annotation-driven validator="validator"/>

I was not aware of it, kind regards

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 21, 2012

Rossen Stoyanchev commented

Ok, thanks for verifying. I am resolving this issue for now until it can be demonstrated.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 21, 2012

Angel commented

I confirm the issue was related to the usage of this:

<mvc:annotation-driven />

instead of this:

<mvc:annotation-driven validator="validator"/>

I think this point in the doucmentation: http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/spring-framework-reference.html#validation-mvc-jsr303 might be causing confussion about how the convention over configuration is handled...

@spring-projects-issues spring-projects-issues added type: bug in: web labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug label Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web
Projects
None yet
Development

No branches or pull requests

2 participants