I want to replace {0} with a field name in Validation error on Spring Web

Asked 1 years ago, Updated 1 years ago, 53 views

I am using the validation function of Spring Web.
To include the field name in the error message, we implement it as follows:

import javax.validation.constraints.NotEmpty;
...abbreviated...
US>MyForm{
    /** US>Name*/
    @NotEmpty()
    private String name;
}
Enter javax.validation.constraints.NotEmpty.message={0}

If MyForm.name is empty, the message "Please enter {0}" appears, but I would like to replace the {0} part with the field name "Name".

Defining "name=name" or "MyForm.name=name" in message.properties did not work.

How can I replace it?

I am using Spring Boot 2.6.9.

java spring spring-boot

2022-09-30 20:22

1 Answers

You may need to include a code to reproduce the problem.
The code I have is working as expected.

To messages.properties

Enter javax.validation.constraints.NotEmpty.message={0}
name = name

"Please enter a name" as expected by adding the .

If messages.properties is present or not, check the behavior of the name property settings.


2022-09-30 20:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.