4

I write custom message interpolation. I want that JPA will use my custom message interpolation. Here http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html/validator-bootstrapping.html#section-message-interpolator I found following description:

Configuration<?> configuration = Validation.byDefaultProvider().configure();
ValidatorFactory factory = configuration
    .messageInterpolator(new ValueFormatterMessageInterpolator(configuration.getDefaultMessageInterpolator()))
    .buildValidatorFactory();

Validator validator = factory.getValidator();

but where I should write such a code? In the web.xml in init-servlet? Can I provide such code in persistance.xml?

P.S. I copy&paste the code. In my case line

ValueFormatterMessageInterpolator(configuration.getDefaultMessageInterpolator()))

will be change on something like this

CustomMessageInterpolator(configuration.getDefaultMessageInterpolator()))

see also How do I dynamically resolve message parameters with Hibernate Validator?

Community
  • 1
  • 1
alexsmail
  • 5,661
  • 7
  • 37
  • 57

1 Answers1

1

The JSR-303 bean validation framework provides the possibility to configure the validatation framework via XML.

E.g. META-INF/validation.xml

See chapter

4.4.6. XML Configuration: META-INF/validation.xml

of the spec for details: http://download.oracle.com/otndocs/jcp/bean_validation-1.0-fr-oth-JSpec/

4.4.6. XML Configuration: META-INF/validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<validation-config
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">
    <default-provider>com.acme.ACMEProvider</default-provider>
    <message-interpolator>com.acme.ACMEAwareMessageInterpolator</message-interpolator>
    <constraint-mapping>META-INF/validation/order-constraints.xml</constraint-mapping>
    <constraint-mapping>META-INF/validation/catalog-constraints.xml</constraint-mapping>
    <constraint-mapping>META-INF/validation/customer-constraints.xml</constraint-mapping>
    <property name="com.acme.validation.logging">WARN</property>
    <property name="com.acme.validation.safetyChecking">failOnError</property>
</validation-config>

Package the xml file with your persistence jar (META-INF/validation.xml) and it should work.

Depending on your deployment packaging (e.g. EAR) it might be necessary to put it in a shared lib in the EAR's lib folder.

The hibernate documentation says:

The key to enable XML configuration for Hibernate Validator is the file validation.xml. If this file exists in the classpath its configuration will be applied when the ValidationFactory gets created. Example 4.1, “validation-configuration-1.0.xsd” shows a model view of the xsd valiation.xml has to adhere to.

http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/#d0e1867

René Link
  • 48,224
  • 13
  • 108
  • 140
  • How can I do it with JPA/Hibernate? – alexsmail Aug 20 '13 at 08:12
  • I have updated my answer. I think that you just have to place it on the classpath and it will be picked up by the validation framework. Nevertheless you can still create a validator and override the xml configuration. – René Link Aug 20 '13 at 08:34