Spring - How to remove a `FieldError` from a BindingResult?

Here is an example that implements @GreyBeardedGuy Answer, Suppose you want to remove the error linked to a field called specialField in the class MyModel with a modelAttribute name as myModel from BindingResult result:

List<FieldError> errorsToKeep = result.getFieldErrors().stream()
                .filter(fer -> !fer.getField().equals("specialField "))
                .collect(Collectors.toList());

        result = new BeanPropertyBindingResult(vacancyDTO, "vacancyDTO");

        for (FieldError fieldError : errorsToKeep) {
            result.addError(fieldError);
        }

Well, first of all, BindingResult is an interface, not a concrete class, and the interface doesn't specify any way to remove an error.

Depending on which implementation of the interface you are dealing with, there may be a method (beyond what's specified in the BindingResult interface) to do this, but it seems unlikely.

The only thing that I can think of is to create a new BindingResult instance, then loop through the errors and re-create all but the one that you want to ignore in the new one.

Tags:

Java

Spring