Announcement Announcement Module
Collapse
No announcement yet.
How to include JSR-303 validation annotation's attribute into validation message? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to include JSR-303 validation annotation's attribute into validation message?

    How to include validation annotation's attribute into validation message?

    There are snapshots from my application below:

    Form Object:
    ...
    @Size(min = 6)
    private String password;
    ...
    validation.properties:
    Size.password = Password should be at least {min} characters
    Without {min} message is displayed successfully on the page, if {min} is included I get:
    Caused by: java.lang.IllegalArgumentException: can't parse argument number min
    The key word here is "number". If I write Size.password = Password should be at least {2} characters, actually it works, and min value will be displayed (I found this arg number during inspecting BindingResult object after validation is applied in my controller). But this is weird.

    What is right configuration for spring web mvc project to able support for validation annotation attribute's values in the message source?

    Thank you.
    Last edited by Aliaksandr; Jan 8th, 2012, 04:38 PM.

  • #2
    Hello

    Next time check JSR-303 documentation

    @Size(min=3, max=60, message="{validation.youFieldName.Size.message}")

    Comment


    • #3
      Ok. Thank you for your time.

      Again when I tried to use:
      @Size(min = 6, message = "{validation.message.Size.password}")
      private String password;
      with the following message in my validation.properties:
      validation.message.Size.password = Password should be at least {min} characters
      ,
      I've got {validation.message.Size.password} this message directly on the page (under this I mean as it is, not replaced with anything from the property file).

      Also, I found the following code fragment in Spring Enterprise Recipes book:
      Set<ConstraintViolation<Member>> violations = 
      validator.validate(member);
      for (ConstraintViolation<Member> violation : violations) {
      String propertyPath = violation.getPropertyPath().toString();
      String message = violation.getMessage();
      // Add JSR-303 errors to BindingResult
      // This allows Spring to display them in view via a FieldError

      result.addError(new FieldError("member",propertyPath,
      "Invalid "+ propertyPath + "(" + message + ")"));
      }
      Pay attention to the bold text above.

      Finally, I've read the documentation (as you suggested), and created ValidationMessages.properties at the root of my classpath. And added there:
      validation.message.Size.password = Password should be at least {min} characters
      ,
      and only then it worked.

      Is it the expected way to do it? Does Spring not help at all in this case?

      Thank you.

      Comment

      Working...
      X