As shown below, when validating a class with a collection as a field in Spring, what if you want to change the error message at the time of input check separately for each element of the collection? Should I do it?

public ParentForm {
    private List <
ChildForm >
public ChildForm {
    private String hoge;
    private String fuga;

For example, assuming that @NotBlank is set for the Hoge of ChildForm in the above state.

The first hoge is required
The second hoge is mandatory
The third hoge is mandatory
Other hoge is required
I want to realize that the error message output is changed depending on the number of the List, such as


I think there is a way to call Validator directly on the logic side to check the input, but I don't want to make many modifications to the class on the logic side, so I would like to make it an annotation-based input check if possible. I will.

Also, I think it is basic to have all the contents of the collection perform the same check, but the data structure cannot be changed.

If anyone knows about it, would you please teach me?

  • Answer # 1

    I don't think there is a normal way.

    First, the condition for determining the occurrence of an error is true or false, and there is nothing else. Next, the error message generation doesn't include variables, that is, no variables are passed (annotation can't use variables), 1234 can't be determined.

    There may be other methods, but I don't think it's a neat way to write.

    ps: I'm a foreigner.