How Spring validates a collection by element

Asked 2 years ago, Updated 2 years ago, 106 views

What should I do if I want to change the input check error message separately for each element of the collection when validating a class with a collection field, as shown below?

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

For example, suppose you set @NotBlank for ChildForm hoge in the above state.

The first hoge is required.
The second hoge is required.
The third hoge is required.
Other hoge must be entered

I would like to change the error message that appears depending on the number of the list.

I think there is a way for the logic side to directly call the validator to check the input, but I don't really want to make modifications to the logic side class, so I would like to check the input based on annotation if possible.

Also, I think it's basic to have all the contents of the collection check the same, but I can't change the data structure.

If anyone knows, could you please let me know?

java spring

2022-09-30 16:53

1 Answers

I don't think there's a normal way.

First of all, there are no other conditions for determining the occurrence of an error except true or false.Next, the generation of error messages does not include variables, which means that variables are not passed (announcement cannot use variables) or 1234 cannot be determined.

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

ps: I'm a foreigner.


2022-09-30 16:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.