1

I tried the approach mentioned on How get property name in a validation message, but cannot apply it to the following situation.

@Data
public class CompanyRequest {

    @PositiveOrZero(message = "validation.amount.positiveOrZero")
    int quantity;

    // code omitted for brevity

}

Here is my *.properties file having message:

validation.amount.positiveOrZero=The ${fieldName} value must be positive or zero

So, how can I use the property name in the validation message?

1 Answers1

0

${fieldName} could not be retrieved easily inside the annotation without major modifications inside the core mechanism that makes the evaluation.

An easier way to achieve what you want, considering that for each field the name would be different, is the following.

Inside properties file put the property

validation.amount.positiveOrZero= value must be positive or zero

And then define the annotation in the code as

@PositiveOrZero(message = "The quantity {validation.amount.positiveOrZero}")
int quantity;

Edit: It seems that the property should not be defined in a simple application.properties file but in another file named ValidationMessages.properties in the classpath.

As documented here

setValidationMessageSource(MessageSource messageSource) Specify a custom Spring MessageSource for resolving validation messages, instead of relying on JSR-303's default "ValidationMessages.properties" bundle in the classpath.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • What about the parameter in the `properties` file? –  Mar 25 '22 at 15:56
  • @stack you need `{}` so that it loads the message from properties file. Where have you found in documentation that you can use the `${fieldName}` inside the message provided to annotation? – Panagiotis Bougioukos Mar 25 '22 at 16:00
  • It returns `"{validation.amount.positiveOrZero}"`. I am not sure if I use the parameter correctly in the properties file. I used as `validation.amount.positiveOrZero=The ${fieldName} value must be positive or zero` –  Mar 25 '22 at 16:01
  • Normally I could read the text in the properties as `@PositiveOrZero(message = "validation.amount.positiveOrZero")` without any problem (not using curly braces). ANy idea? –  Mar 25 '22 at 16:03
  • @stack If you use `@PositiveOrZero(message = "validation.amount.positiveOrZero")` what does it output on validation failure? – Panagiotis Bougioukos Mar 25 '22 at 16:04
  • If I use `validation.amount.positiveOrZero=The ${fieldName} value must be positive or zero` in the prop file, it gives *"org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [1] in public org.springframework."* error. –  Mar 25 '22 at 16:16
  • But, if I use `validation.amount.positiveOrZero=The value must be positive or zero` in the prop file, it gives *"The value must be positive or zero"* –  Mar 25 '22 at 16:17
  • Any help please? –  Mar 25 '22 at 16:21
  • @stack check the updated answer – Panagiotis Bougioukos Mar 25 '22 at 16:26
  • Thanks a lot for your helps. But unfortunately it gives *"The quantity ${validation.amount.positiveOrZero}"* –  Mar 25 '22 at 16:31
  • @stack updated answer, please try it without the `$` inside annotation – Panagiotis Bougioukos Mar 25 '22 at 16:43
  • It gives *"The quantity {validation.amount.positiveOrZero}"*. –  Mar 25 '22 at 16:48
  • I think we need to interpolate the variable rather than the message in properties file. I need to interpolate the name of the field and the message in properties. Something like `@PositiveOrZero(message = "${validatedValue} validation.amount.positiveOrZero")` –  Mar 25 '22 at 16:50
  • @stack No, this will pass the value, example : `-1 value must be positive or zero` . Check if the property is correctly defined in properties file – Panagiotis Bougioukos Mar 25 '22 at 16:56
  • No, I just gave `${validatedValue}` as an example. So, what do you mean with "property is correctly defined in properties file"? Why do not you post all the necessary changes to your answer? –  Mar 25 '22 at 17:04
  • @stack this as posted in my answer `(message = "The quantity {validation.amount.positiveOrZero}")` is expected to work. Seems like the property `validation.amount.positiveOrZero` is not recognized in your project – Panagiotis Bougioukos Mar 25 '22 at 17:37
  • @stack please check the `edit` in my answer – Panagiotis Bougioukos Mar 25 '22 at 17:39
  • 1
    You rock!.. Voted up, but just a little issue. As I stated in my question, I need to grab `fieldName` in the message and create a message by using the name of the field automatically using a interpolation e.g. `${fieldName}`. Can I do that? –  Mar 25 '22 at 18:31
  • @stack this would require more changes in deeper core functionality which may affect other things that you don't want to be affected. That is why I would recommend if it is only for this simple annotation to use this solution – Panagiotis Bougioukos Mar 25 '22 at 18:34
  • Yes, you right. I am just wondering if this solution supports internationalization. Does it? –  Mar 25 '22 at 18:46
  • @stack Yes according to oracle documentation you have to provide a different file for each Language and it would support internalization. https://docs.oracle.com/javaee/7/tutorial/bean-validation-advanced002.htm – Panagiotis Bougioukos Mar 25 '22 at 19:41