12 Replies Latest reply on Jan 9, 2008 7:10 PM by pmuir

    Internationalized hibernate validator messages

       

      "necro" wrote:
      Hi,

      First of all, JBoss Seam is an excelent idea. I've been lookigin at the examples and its just impresive.

      In all of our projects, it's necesary to internationalize applications. I love the idea of annotating classes with validation constraints using hibernate validator, but I can't seem to find a way to internationalize the validation messages. You could pass a validation key to the validation annotation but how would faces render the internationalized message?


      Seam's validation has a specific role in validating data within your application, but if you would like to have actual control over what's being output in the UI layer, you might be better off using JSF validators and associate them appropriately within your view.

      With JSF 1.2, you can do a lot more with specifying multiple resource bundles in the faces-config.xml and pulling them into any value/validator/converter/message in the document.

        • 1. Re: Internationalized hibernate validator messages
          jpaagt

          Thank you for your fast response.

          The idea of using hibernate validator is to define validation in one place and one place only, so that you don't replicate this througout your application. It would be a shame that the Hibernate Validator framework could not be used to validate the user input data because the framework cannot render an internationalized message. Using JSF validation in JSPs would be a solution, but, that "cool" feature (defining validation in one place) would be lost... which in my opinion makes Seam loose a great feature. JSF validation and data validation would have to be synchronized every time data constraints are changed, which is more vulnerable to error. I don't know if I'm asking to much of the framework...

          • 2. Re: Internationalized hibernate validator messages

             

            "jpaagt" wrote:
            Thank you for your fast response.

            The idea of using hibernate validator is to define validation in one place and one place only, so that you don't replicate this througout your application. It would be a shame that the Hibernate Validator framework could not be used to validate the user input data because the framework cannot render an internationalized message. Using JSF validation in JSPs would be a solution, but, that "cool" feature (defining validation in one place) would be lost... which in my opinion makes Seam loose a great feature. JSF validation and data validation would have to be synchronized every time data constraints are changed, which is more vulnerable to error. I don't know if I'm asking to much of the framework...


            What you are stating makes total sense. The JSF spec had to go through the same growing pains into JSF 1.2 for handling customized messages. I would go ahead and use the JIRA issue tracker to post your ideas on how best to express this customization.

            -- Jacob Hookom

            • 3. Re: Internationalized hibernate validator messages
              gavin.king

              Hibernate Validator supports internationalization.

              Check the documentation for Hibernate Validator to see how to include text from a resource bunde in the error message.

              • 4. Re: Internationalized hibernate validator messages
                jpaagt

                Hibernate Validator does have a mechanism to internationalize messages. From what I could see it uses the class ClassValidator to validate an object. ClassValidator has three constructors, two of which are mentioned in the documentation:

                ClassValidator(Class)
                and
                ClassValidator(Class, ResourceBundle)

                as stated in the section 3.2.3 Application-level validation.

                However, JBoss Seam only uses the first constructor that recieves only a class. The class validator is created in class org.jboss.seam.Component in the constructor as follows:
                validator = new ClassValidator(beanClass);
                (line 153).

                However, I'v another more preocupating question bugging me. Should html forms (or user input in general) be validated using Hibernate Validator? Not all forms map one on one with an Entity Bean. So is this feature really useful?. If it is for some cases, how coherent would it be to use Faces validation and Hibernate validation in the same app?...

                • 5. Re: Internationalized hibernate validator messages
                  gavin.king

                  You can use Hibernate Validator for things that are not entities. Actually, most form fields of most forms map directly to an attribute of an entity. But those that don't can still be handled by HV.

                  I'll add an issue to JIRA to allow a resource bundle to be used.

                  • 6. Re: Internationalized hibernate validator messages
                    gavin.king

                    Fixed in CVS, you can now put messages in

                    validator_en.properties
                    validator_de.properties

                    etc

                    • 7. Re: Internationalized hibernate validator messages
                      mirko27

                      So now we should create validation messages through Seam?
                      Or what? And where to put them? any dependencies on names?
                      Seam is still very poorly documented:(

                      • 8. Re: Internationalized hibernate validator messages JBSEAM-86
                        nhpvti

                        Hi,

                        JBSEAM-86 issue states that the issue has been fixed. Bu it doesn't work for me in JBoss-Seam 1.0.1.GA

                        server.log has e.g. such lines:

                        DEBUG [org.hibernate.validator.ClassValidator] ResourceBundle ValidatorMessages not found in Validator classloader. Delegate to org.hibernate.validator.resources.DefaultValidatorMessages

                        Application shows for the booking example e.g. #"creditCard": Eingabe erforderlich#.

                        But DefaultValidatorMessages_de.properties in \jboss-4.0.4.GA\server\default\lib\hibernate-annotations.jar\org\hibernate\validator\resources\

                        has the following entry:
                        validator.notNull=kann nicht leer sein

                        Where does the text #"creditCard": Eingabe erforderlich# comes from and how it can be replaced by a customized one?

                        Thank you in advance for any tip

                        • 9. Re: Internationalized hibernate validator messages
                          nhpvti

                          Additional info:

                          I've placed

                          validator.properties
                          validator_en.properties
                          validator_de.properties

                          into

                          \jboss-seam-1.0.1.GA\examples\booking\resources\WEB-INF\classes

                          But they are ignored

                          • 10. Re: Internationalized hibernate validator messages JBSEAM-86
                            nhpvti

                             

                            "nhpvti" wrote:
                            Hi,

                            JBSEAM-86 issue states that the issue has been fixed. Bu it doesn't work for me in JBoss-Seam 1.0.1.GA



                            My error was placing ValidatorMessages properties files in WEB-INF/classes directory. They shoul be placed at the root of the EAR file.

                            See discussion on the same topic in the following thread
                            http://www.jboss.com/index.html?module=bb&op=viewtopic&t=88798


                            • 11. Properties file and subdirectories?

                              Hi,

                              i use hibernate validator to annotate and validate the domain model. After searching in this forum i found no real solution to the question "where to put the ValidatorMessages.properties file".

                              I wrote several own validators so i had to introduce such a resource bundle. I put the file into src/main/resources (default maven structure). In this situation the file is found by hibernate validator. But i don't want to "pollute" the root-directory of src/main/resources with localization files, so i put them in a sub directory. In this situation the properties file is not found anymore.
                              How can hibernate validator find the file located in a subdirectory (without touching the original jar file)?

                              Any remarks?
                              Thanks in advance, Ben

                              • 12. Re: Internationalized hibernate validator messages
                                pmuir

                                It can't, it's hard coded to ValidatorMessages.properties.

                                BUT, Seam uses the Seam resource bundle as the message bundle for Hibernate Validator, which does allow you to place resource files where ever you like.