Search code examples
javajpavalidationbean-validationresourcebundle

Bean Validation + Resource Bundle ideas?


Here is what i want to be able to do with bean validation in my JPA entities :

  1. Instead of @NotNull, i would like to do @NotNull(message="keyInMyResourceBundleFile")
  2. I would like also to parameterize the resourceBundle, and i dont know the syntax for it, because it seems to me the message attribute contains only a string.
  3. The parameter itself could be i18n-ed. For example, assuming there's a param attribute for the resourcebundle parameters, in English, @NotNull(message="missing.value", params={"credit card"}) String creditCard; It will be displayed something like this : "Missing required value for field credit card. In Indonesia, it'll be something like "Nilai harus di isi untuk Kartu Kredit. In this example, i cant hardcode the "credit card" because in Indonesia, it's "Kartu Kredit"
  4. Displays the error message defined in the resource bundle on the log file or UI. Im not sure the way on how to do it, should i catch the ConstraintViolationException, get the message, and process the resource bundle by my own code ?

Please share your thoughts on this ?

Thank you !


Solution

  • Regarding 1 + 2

    @NotNull(message="{keyInMyResourceBundleFile}")
    

    Curly brackets are the indicator of a parameter substitution

    Regarding 3

    No idea what you are after. There is no params attribute for @NotNull. I guess you would do

    @NotNull(message="{missing.credit.card}")

    And of if you place it on another property you would call the key {missing.name}

    Regarding 4

    The ConstraintViolationException contains the set of *ConstraintViolation*s. Each ConstraintViolation contains the interpolated message as well as the message template. If you want to log it, do it ...