Results 1 to 6 of 6

Thread: JSR-303 and error codes

  1. #1
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    37

    Default 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. #2
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    37

    Default

    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 at 05:46 PM. Reason: Added jira link

  3. #3
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    37

    Default

    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!

  4. #4
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Thumbs up

    Quote 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!
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  5. #5
    Join Date
    Nov 2007
    Location
    Belarus
    Posts
    72

    Default

    Currently brackets are still needed. Is this by design?
    Alexander Semenov

    My Jabber ID: bohtvaroh@jabby.org

  6. #6
    Join Date
    Apr 2011
    Posts
    12

    Default

    any way to use parametric messages?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •