-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Validator: using the built-in constraints with custom types
PostPosted: Mon Mar 04, 2013 1:01 pm 
Newbie

Joined: Mon Mar 04, 2013 12:48 pm
Posts: 6
Hello folks,

i would like to be able to use the validator to validate custom types with the built-in constraints, for example:

Code:
@Length(min=5,max=10)
private MyTextField myField;


I already know that a new validator implementation shoud be created that validates the Length constraint on MyTextField types:

Code:
public class LengthValidatorForMyTextField implements ConstraintValidator<Length, MyTextField> { ... }


The question is, how could i register my new validator to be picked up and used when a MyTextField type is encountered.

Of course i could re-create all the necessary constraints but using the built-in ones would be far more elegant.


Top
 Profile  
 
 Post subject: Re: Validator: using the built-in constraints with custom types
PostPosted: Mon Mar 04, 2013 5:19 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

you can register your validator using an XML constraint mapping:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
    xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
   
    <constraint-definition annotation="org.hibernate.validator.constraints.Length">
        <validated-by include-existing-validators="true">
            <value>org.mycompany.LengthValidatorForMyTextField</value>
        </validated-by>
    </constraint-definition>
</constraint-mappings>


This mapping you can either register in META-INF/validation.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <constraint-mapping>/path/to/mapping/my-text-field-mapping.xml</constraint-mapping>
</validation-config>


Or pass it as InputStream when programmatically bootstrapping your validator:

Code:
InputStream mappingStream = ...;
Validator validator = Validation
    .byDefaultProvider()
    .configure()
    .addMapping( mappingStream )
    .buildValidatorFactory()
    .getValidator();


You can learn more in the Hibernate Validator reference guide (http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-xmlconfiguration).

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validator: using the built-in constraints with custom types
PostPosted: Mon Mar 04, 2013 9:40 pm 
Newbie

Joined: Mon Mar 04, 2013 12:48 pm
Posts: 6
Thank you very much for your answer, that's exactly what i was looking for!


Top
 Profile  
 
 Post subject: Re: Validator: using the built-in constraints with custom types
PostPosted: Tue Mar 05, 2013 5:43 am 
Newbie

Joined: Mon Mar 04, 2013 12:48 pm
Posts: 6
The solution you've suggested works perfectly, thanks again. However i have another problem. I use HV in an OSGi environment (equinox, RCP), and not able to load the custom validators properly. I can provide the XML mapping file properly using the "platform://" URL scheme, that works well, however the defined classes cannot be loaded by the XML parser engine.

Code:
javax.validation.ValidationException: HV000065: Unable to load class: hu.glh.framework.validators.swt.LengthValidatorForText.
   at org.hibernate.validator.internal.util.privilegedactions.LoadClass.loadNonValidatorClass(LoadClass.java:100)
   at org.hibernate.validator.internal.util.privilegedactions.LoadClass.run(LoadClass.java:53)
   at org.hibernate.validator.internal.util.ReflectionHelper.loadClass(ReflectionHelper.java:129)
   at org.hibernate.validator.internal.xml.XmlMappingParser.parseConstraintDefinitions(XmlMappingParser.java:170)
   at org.hibernate.validator.internal.xml.XmlMappingParser.parse(XmlMappingParser.java:103)
   at org.hibernate.validator.internal.metadata.provider.XmlMetaDataProvider.<init>(XmlMetaDataProvider.java:63)
   at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.<init>(ValidatorFactoryImpl.java:69)
   at org.hibernate.validator.HibernateValidator.buildValidatorFactory(HibernateValidator.java:45)
   at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:178)
   at com.evonik.epic.client.ui.parts.FooPart$1.widgetSelected(FooPart.java:90)
   at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
   at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
   at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
   at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
   at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
   at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1029)
   at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
   at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:923)
   at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
   at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:150)
   at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
   at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
   at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
   at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
   at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
   at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Caused by: java.lang.ClassNotFoundException: hu.glh.framework.validators.swt.LengthValidatorForText
   at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
   at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
   at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
   at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Class.java:266)
   at org.hibernate.validator.internal.util.privilegedactions.LoadClass.loadNonValidatorClass(LoadClass.java:97)
   ... 32 more


I've searched for a solution, and found this ticket:

https://hibernate.onjira.com/browse/HV-582

My question is that has any progress on this topic been made yet? Or what are my options to work this issue around?

I've created a fragment plugin with host "org.hibernate.validator" and put my validator classes there, which is in fact works quite OK, but there is a big downside: no other plugins can reference the validator classes, therefore i do not consider this workaround as a solution.


Top
 Profile  
 
 Post subject: Re: Validator: using the built-in constraints with custom types
PostPosted: Tue Mar 05, 2013 5:57 am 
Newbie

Joined: Mon Mar 04, 2013 12:48 pm
Posts: 6
I could solve the issue with the fragment plugin. Instead of putting the validators in the plugin, it only has a reference to the bundle that contains the validators, and it also re-exports the packages in it. This way the classes can be loaded properly, and other plugins are able to reference the validator classes.

Unfortunately this workaround introduces a dependency cycle so "allow binary cycles" should be checked when exporting the RCP product.


Top
 Profile  
 
 Post subject: Re: Validator: using the built-in constraints with custom types
PostPosted: Wed Mar 06, 2013 5:49 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

Great that you found a solution which works for you.

You might be interested in tracking https://hibernate.onjira.com/browse/HV-570 which is the issue for making Hibernate Validator more OSGi friendly. This should hopefully be fixed with one of the next HV 5.x releases.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.