Results 1 to 3 of 3

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

Hybrid View

  1. #1
    Join Date
    Mar 2008
    Posts
    29

    Exclamation 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 at 03:38 PM.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,806

    Default

    Hello

    Next time check JSR-303 documentation

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

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Mar 2008
    Posts
    29

    Default

    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.

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
  •