Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: can i change the name of the ValidationMessages.properties?
PostPosted: Tue Jun 28, 2011 5:34 am 
Newbie

Joined: Tue Jun 28, 2011 5:19 am
Posts: 1
Dear All,
i am using hibernate validation + JSR 303 validation with JSF 2 and RichFaces 4

when i add annotations on my bean attributes, i get the messages from the ValidationMessages.properties on the default (root) package, assume the case as folllow:

class MyBean{
@NotEmpty(message="{Name_Error_Message}")
private String name;
// rest of code here
}

ValidationMessages.properties
Name_Error_Message = The name cannot be empty

my question is: can i change the name of the ValidationMessages.properties to any name i want? if yes, HOW?

Regards.

Mohd Noor


Top
 Profile  
 
 Post subject: Re: can i change the name of the ValidationMessages.properties?
PostPosted: Wed Jun 29, 2011 11:38 am 
Contributor
Contributor

Joined: Mon Feb 28, 2011 7:27 pm
Posts: 20
Location: France
Hi,

Yes you can :)

To do that you have to write your own MessageInterpolator which delegates the message interpolation to Hibernate Validator ResourceBundleMessageInterpolator. This ResourceBundleMessageInterpolator have to be initialized with a custom ResourceBundleLocator or with an implementation provided by HV (see [1]). This ResourceBundleLocator will be responsible to locate your custom ResourceBundle. After you'll have to create a validation configuration file named validation.xml[2] which references your custom MessageInterpolator implementation class.

Hope this help

--Kevin

PS: For Hibernate Validator related questions please use this forum viewforum.php?f=9

[1] http://docs.jboss.org/hibernate/validat ... le-locator
[2] http://docs.jboss.org/hibernate/validat ... e/#d0e2053
[3] This post might help viewtopic.php?f=9&t=1010033


Top
 Profile  
 
 Post subject: Re: can i change the name of the ValidationMessages.properties?
PostPosted: Mon Jul 18, 2011 8:31 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@kevin,
I think we could offer a property in Hibernate Validator to point to the filename as it's likely a common enough use case and writing a new interpolator is a bit overkill for this.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: can i change the name of the ValidationMessages.properties?
PostPosted: Mon Jul 18, 2011 8:32 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I've created https://hibernate.onjira.com/browse/HV-507

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: can i change the name of the ValidationMessages.properties?
PostPosted: Mon Jul 18, 2011 3:45 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 386
Actually it's not necessary to implement a new message interpolator. Using HV's ResourceBundleMessageInterpolator and its bundle locator API a custom bundle name can be specified like this:

Code:
Validator validator =
   Validation.byProvider(HibernateValidator.class)
      .configure()
      .messageInterpolator(
         new ResourceBundleMessageInterpolator(
            new PlatformResourceBundleLocator( "my_bundle" )))
      .buildValidatorFactory()
      .getValidator();

This doen't work using the XML configuration at the moment, though. I generally like the property approach, but I'm slightly concerned about possible inconsistent configurations (specifying the property but using a different message interpolator etc.).

--Gunnar

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 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.