Announcement Announcement Module
Collapse
No announcement yet.
Validation error is appended with field path Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validation error is appended with field path

    Hi all, I posted this already in other thread, but still no reply

    I reject a value in validator with
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "price", "validation.field.requred");

    I display error on JSP with <form:errors path="price" cssClass="fieldError"/> inside <form:form commandName="item">

    And i would like the error code to be just "validation.field.requred",
    but i get "validation.field.required.item.price"

    There should be easy solution for this I think, Thanks beforehand

  • #2
    Spring has a class called DefaultMessageCodesResolver which generates a series of codes which can be used to generate error messages. This provides flexibility in producing customized messages from a single error code. See the javadoc for explanation. If you just provide a resource bundle with the message code you want to be used then all the extra codes will be ignored. If you really want to remove the extra error codes you could set the MessageCodeResolver on the DataBinder with your own implementation.

    Comment


    • #3
      thanks for reply,

      the thing is that i want <form:errors> tag to query different code (which does not include path)... if i myself understand good what i want

      Comment


      • #4
        the spring errors tag used the underlying MessageSource. Now the great thing with bind errors/validation errors is that you Spring looks for different message codes.

        If you specify validation.field.requred The errors tag will try to find one of the following messages

        validation.field.requred
        validation.field.requred.price
        validation.field.requred.item.price

        It starts with the most restrictive one 'validation.field.requred.item.price' and then relaxes it more and more. Until one is found, if none is found it displays the most restrictive one.

        So now when you include one of those message codes in your messages.properties file Spring will resolve the message. The code below in your messages.properties is enough.

        Code:
        validation.field.requred=This field must be filled.
        Check chapter 3.8.1 and chapter 5.3 about MessageSource and how spring resolves error codes.

        Comment


        • #5
          he, that's what i was looking for after reading pointed chapters... but id doesn't work in my case...

          Is this "relaxation" handled inside MessageSource??? I've got custom MessageSource implementation which uses DB for i18n. So I have to implement "relaxation" inside my MessageSource? Ok, i'll have a look into ResourceBundleMessageSource sources. Thanks!

          Comment


          • #6
            The BindException class takes care of that. So as long as the messages are there they should be resolved. For the purpose of testing you might try to switch to a ResourceBundleMessage source to check if it works. If it does, you might have an issue with your own custom implementation.

            Comment


            • #7
              Solved. Thanks!

              Problem was in my implementation - I implemented "return of code as default message" right inside resolveCode method while had to return null and just set "useCodeAsDefaultMessage" to true.

              Comment

              Working...
              X