14 Replies Latest reply on Sep 12, 2006 3:27 PM by Manfred Moser

    i18n entity validation message?

    Manfred Moser Newbie

      Hi!

      I need to find a way to internationalise the error message for a specific property on an entity bean. All the examples I found use static text like this

      @NotNull
      @Length(min = 2, message="Lastname has to be 2 characters of longer.")
      public String getLastName()
      {
      return lastName;
      }

      I would like to internationalise these message (like the rest of the app) and would prefer to be able to keep the concern of the message within the entity. So I tried this

      @In(create = true)
      private transient ResourceBundle resourceBundle;

      @NotNull
      @Length(min = 2, message=resourceBundle.getString("person.lastName.length"))
      public String getLastName()
      {
      return lastName;
      }

      but unfortunately the message has to be a constant expression so that does not work. Ideally something like this would work, where the message is automagically taken from the bundle.

      @NotNull
      @Length(min = 2, message=person.lastName.length))
      public String getLastName()
      {
      return lastName;
      }

      Any pointers would be great. Also let me know if you think I should not have any of those messages linked to the entity directly even though the defaults will always kick in..

        • 1. Re: i18n entity validation message?
          Maurice Zeijen Master

          I found this in the Hibernate Validator chapter of the Hibernate Annotations documentary.

          4.1.3. Error messages

          Hibernate Validator comes with a default set of error messages translated in a few languages (if yours is not part of it, please sent us a patch). You can override those messages by creating a ValidatorMessages.properties or (ValidatorMessages_loc.properties) out of org.hibernate.validator.resources.DefaultValidatorMessages.properties and change the appropriate keys. You can even add your own additional set of messages while writing your validator annotations.

          Alternatively you can provide a ResourceBundle while checking programmatically the validation rules on a bean.


          You can find the complete chapter here:
          http://www.hibernate.org/hib_docs/annotations/reference/en/html/validator.html.

          I hope this helps you.

          • 2. Re: i18n entity validation message?
            Manfred Moser Newbie

            Thanks Smies. I guess I should give myself a RTFM. Oh well. I have read that documentation and found something very useful. To achieve my desired behaviour the notation is

            @NotNull
            @Length(min = 2, message="{person.lastName.length}"))
            public String getLastName()
            {
            return lastName;
            }

            Notice the squiggly brackets around the property in apostrohies!

            However to make this actually work well you need a very bad setup. You need to have the Hibernate ValidatorMessages.properties files as well as the Seam messages.properties files to exist and contain all properties. If you only use the beans via Seam you can get away with the ValidatorMessages files only containing empty property definitions like this

            validator.length=

            so that at least you still have only one resource bundle with all the i18n texts.

            As an alternative for now I am thinking of copying the message.properties files to ValidatorMessage.properties files as part of the build but surely there must be a better way.

            Once we have a proper way this should go into the Seam wiki and one of the examples I suppose (or the upcoming book). I am happy to help with that.

            manfred



            • 3. Re: i18n entity validation message?
              Manfred Moser Newbie

              I have added an ant task to copy the messages files to ValidatorMessages files as part of the build for now. Provided your messages files contain the validator.* and all other validator messages you have to only maintain the message files and still have all messages available in both seam and hibernate annotation.

              <copy todir="${messages.properties.path}">
               <fileset dir="${messages.properties.path}"/>
               <globmapper from="messages*" to="ValidatorMessages*"/>
               </copy>


              Not ideal but that works.

              manfred

              • 4. Re: i18n entity validation message?
                Manfred Moser Newbie

                Just a fyi - I created a JIRA issue http://jira.jboss.org/jira/browse/JBSEAM-340 to get this streamlined.

                • 5. Re: i18n entity validation message?
                  Raja Master

                  I dont think you need to do build level changes. If you have a ValidatorMessages.properties (and ValidatorMessages in other locales) available in the Current ClassLoader, Hibernate should be able to use that instead of the default validator messages that come bundled with it. Atleast thats how my reading of the Hibernate code indicates. I havent tried this, but if you have a ValidatorMessages.properties in ur EAR archive, that should suffice.

                  • 6. Re: i18n entity validation message?
                    Manfred Moser Newbie

                    If you want the properties to be available in Seam AND Hibernate you need to maintain the properties in both bundles. I have tried and tested it and thats the only way to actually get it to work.

                    • 7. Re: i18n entity validation message?
                      Raja Master

                      Sorry, I dint understand the request clearly.

                      Anyway, you can configure Seam to use the same ValidatorMessages.properties by configurng the components.xml for the "resourceBundle" component and specifying the "bundleName" with ValidatorMessages. That way, youd only have one bundle file for both Seam and Hibernate. Something like this is components.xml


                      ValidatorMessages

                      • 8. Re: i18n entity validation message?
                        Raja Master

                        Sorry, the last answer dint come up quite well in the instant reply. Heres a sample config of components.xml

                         <component name="resourceBundle">
                         <property name="bundleName">ValidatorMessages</property>
                         </component>
                        


                        • 9. Re: i18n entity validation message?
                          Manfred Moser Newbie

                          Thanks Raja. Thats good to know. I will probably migrate to that approach for now. However it would still be better for Seam to handle that a bit more cleanly and have an example of it in the code base.

                          • 10. Re: i18n entity validation message?
                            Valerij Timofeev Newbie

                             

                            "mosabua" wrote:

                            @NotNull
                            @Length(min = 2, message="{person.lastName.length}"))
                            public String getLastName()
                            {
                            return lastName;
                            }



                            A seam-gen generated scaffold application relies only on the Seam properties files, for example :

                            @NotNull
                            @Length(min = 1, max = 50, message = "#{messages['err.fullname.length']}")
                            public String getFullname()
                            {
                            return fullname;
                            }

                            According entry in a properties file can look so:

                            err.fullname.length = Fullname is mandatory and should be maximal 50 characters long

                            The problem is obvious: max length information is spread among two places :-(

                            The following form unfortunately doesn't work

                            err.fullname.length = Fullname is mandatory and should be maximal {max} characters long

                            I suppose this could be a feature request: Seam messages must be able to interpret hibernate-validator alike variables {max}

                            • 11. Re: i18n entity validation message?
                              Valerij Timofeev Newbie

                               

                              "mosabua" wrote:
                              Thanks Raja. Thats good to know. I will probably migrate to that approach for now. However it would still be better for Seam to handle that a bit more cleanly and have an example of it in the code base.


                              Works good if all the error messages are required to be displayed on the only place.

                              Tried to use h:message tag for displaying error message close to according input field
                              <td><h:inputSecret id="userPassword" value="#{user.password}" required="true" /> <h:message for="userPassword"/></td>

                              Unfortunately default hibernate message with non-user-friendly id is shown in case if the field is empty. And if number of characters is not within allowed range, custom global message is shown.

                              Question to JBoss Seam developers: is there any way in Seam to show localized error message close to the input field causing this error?

                              • 12. Re: i18n entity validation message?
                                Gavin King Master

                                Well, this is really a JSF question. I guess you can probably do whatever you like by writing some custom JSF tags.

                                • 13. Re: i18n entity validation message?
                                  Philip Planinsky Newbie

                                  Hi,

                                  I have tried to use notation like:
                                  @Length(min=3, max=70, message="{testcustommessage}")
                                  where testcustommessage is part of messages.properties.

                                  My set up is:
                                  * jboss 4.0.4GA (JEMS)
                                  * SEAM 1.0.1GA
                                  * SEAM booking application with messages properties added:

                                  validator.assertFalse=assertion failed
                                  validator.assertTrue=assertion failed
                                  validator.future=must be a future date
                                  validator.length=Length should be {min} and {max}
                                  validator.max=must be less than or equal to {value}
                                  validator.min=must be greater than or equal to {value}
                                  validator.notNull=Not Null
                                  validator.past=must be a past date
                                  validator.pattern=must match "{regex}"
                                  validator.range=must be between {min} and {max}
                                  validator.size=size must be between {min} and {max}
                                  validator.email=not a well-formed email address
                                  testcustommessage=Test custom messages


                                  I have these validators in the properties file, otherwise it won't work at all.
                                  (I have understood that this is some problem in SEAM)

                                  1. I am able to show the testcustommmessage in an xhtml page without a problem.
                                  2. I can also use testcustommessage with FacesMessages.

                                  3. When I try to use it in the @Length validator I always get an exception on JBOSS startup:

                                  Caused by: java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key testcustommessage
                                  at java.util.ResourceBundle.getObject(ResourceBundle.java:325)
                                  at java.util.ResourceBundle.getString(ResourceBundle.java:285)
                                  at org.hibernate.validator.ClassValidator.replace(ClassValidator.java:518)
                                  at org.hibernate.validator.ClassValidator.createValidator(ClassValidator.java:260)

                                  I have tried to use:

                                  @Length(min=3, max=70, message="{messages.testcustommessage}")

                                  but it doesn't work too.

                                  I have also tried with ValidatorMessages.properties in EJB root, META-INF, etc. None with success. Still getting the same exception.

                                  Where I have to put the message.properties or ValidatorMessages.properties?

                                  I am sure I am missing something, but I have started with Jboss a couple of weeks ago and I am not able to find the problem.
                                  Please, can somebody help?

                                  Thanks,
                                  Philip

                                  P.S.
                                  I would be great if there are some documents and examples about htis problem and workarounds. It would be of great help to starters.

                                  • 14. Re: i18n entity validation message?
                                    Manfred Moser Newbie

                                    From my experience you have to have all validation properties in messages* and ValidatorMessages* properties.