Announcement Announcement Module
Collapse
No announcement yet.
JSR-303 and error codes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSR-303 and error codes

    Hi everyone,

    I played a little bit with JSR-303 and relative support in Spring...

    I wasn't able to have my error codes translated.

    My old code was something like:

    Code:
    public class SignupBeanValidator implements Validator {
    
        public void validate(Object target, Errors errors) {
    
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "empty_field");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "empty_field");
        
        }
    
    }
    When validation errors occurred, the relative code (in this exaple, "empty_field") was correctly translated using the correct Locale and the value in my i18n bundle (example "message_en.properties").

    Now my code looks like:

    Code:
    public class SignupBean {
    
        @NotEmpty(message = "empty_field")
        private String username;
    
        @NotEmpty(message = "empty_field")
        private String password;
    
    }
    But I always get the default error String ("may not be empty").

    I'm aware that the Hibernate JSR-303 RI looks for ValidationMessages.properties in the root of the classpath, but:
    • I don't know how to do Locale resolution. Is this supported by Spring?
    • I would like NOT to replicate again my validation error messages, since they are already in my i18n bundles, nicely

    Thanks!

  • #2
    I answer my own questions

    1) Yes, Spring wraps the MessageInterpolator and passes the correct Locale. Just name your bundles "ValidationMessages_locale.properties" (example ValidationMessages_en.properties)

    2) You cannot (or, at least, I wasn't able to) tell the Hibernate RI where to look for message bundles. It will always look for ValidationMessages.properties in the root of the classpath Sooo bad.

    (Hibernate JIRA for this issue http://opensource.atlassian.com/proj.../browse/HV-238)

    Anyway, there's still a problem. It doesn't like my custom messages, so that

    Code:
    @NotEmpty(message = "empty_field")
    always comes out as "empty_field" and doesn't get translated.

    If I drop the "message" property, it will look for the default message code "org.hibernate.validator.constraints.NotEmpty.mess age", and will work.

    Any idea?
    Last edited by namero999; Jan 10th, 2010, 05:46 PM. Reason: Added jira link

    Comment


    • #3
      Just put the name of the field inside curly braces

      Code:
      @NotEmpty(message = "{empty_field}")
      Sorry for this self-ish thread, I wrote on the forum too soon without deeply investigate the problem. Anyway, hope this helps someone!

      Comment


      • #4
        Originally posted by namero999 View Post
        Sorry for this self-ish thread, I wrote on the forum too soon without deeply investigate the problem. Anyway, hope this helps someone!
        Don't apologise; this solved my problem, thanks!

        Comment


        • #6
          any way to use parametric messages?

          Comment

          Working...
          X