validation - How to configure custom message interpolation with Hibernate? -
i write custom message interpolation. want jpa use custom message interpolation. here http://docs.jboss.org/hibernate/validator/4.2/reference/en-us/html/validator-bootstrapping.html#section-message-interpolator found following description:
configuration<?> configuration = validation.bydefaultprovider().configure(); validatorfactory factory = configuration .messageinterpolator(new valueformattermessageinterpolator(configuration.getdefaultmessageinterpolator())) .buildvalidatorfactory(); validator validator = factory.getvalidator();
but should write such code? in web.xml in init-servlet? can provide such code in persistance.xml?
p.s. copy&paste code. in case line
valueformattermessageinterpolator(configuration.getdefaultmessageinterpolator()))
will change on this
custommessageinterpolator(configuration.getdefaultmessageinterpolator()))
see how dynamically resolve message parameters hibernate validator?
the jsr-303 bean validation framework provides possibility configure validatation framework via xml.
e.g. meta-inf/validation.xml
see chapter
4.4.6. xml configuration: meta-inf/validation.xml
of spec 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 xml file persistence jar (meta-inf/validation.xml) , should work.
depending on deployment packaging (e.g. ear) might necessary put in shared lib in ear's lib folder.
the hibernate documentation says:
the key enable xml configuration hibernate validator file validation.xml. if file exists in classpath configuration applied when validationfactory gets created. example 4.1, “validation-configuration-1.0.xsd” shows model view of xsd valiation.xml has adhere to.
http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/#d0e1867
Comments
Post a Comment