I am using Spring Boot with Jakarta Bean Validation and applying multiple validation rules to fields. However, when multiple constraints are violated for the same field (e.g., NotBlank and Size), all the validation errors are returned. I want to return only the first error message per field.

For example, with this model:

@NotBlank(message = "Name is required")
@Size(min = 2, max = 50, message = "Name must be between 2 and 50 characters")
private String name;

When the name field is empty, I expect only the message "Name is required" to be returned, which is the behavior I want. However, sometimes I also get the "Name must be between 2 and 50 characters" message, even though the field is empty, which is the issue.

I have implemented the following logic in my controller to capture only the first error:

for (var error : result.getFieldErrors()) {
    if (!errors.containsKey(error.getField())) {
        errors.put(error.getField(), error.getDefaultMessage());
    }
}

However, this doesn’t always work as expected. Sometimes, multiple error messages are still returned for the same field. I need a way to ensure only the first validation error (based on the order of the rules) is returned for each field.

How can I modify my approach to ensure that only the first validation error is captured and returned for each field, especially when multiple validation annotations are applied?

Comment From: philwebb

I'm not sure why your approach wouldn't work, but it doesn't seem like this is a bug within Spring Boot. As such, I think this is a question that would be better suited to Stack Overflow. I would suggest providing a minimal, reproducible example so that folks can debug why your existing code doesn't always work.

Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Comment From: wilkinsona

Before asking a question of your own on Stack Overflow, you may also be interested in https://stackoverflow.com/questions/45859113/spring-hibernate-validation-getting-one-error-message-per-field which I think achieves your goal.

Comment From: alizainofficial25

@philwebb Thanks for the suggestion. I’ve already posted a question on Stack Overflow, and it’s currently in staging for review. However, after further investigation, it seems this issue is related to how Spring Boot handles multiple validation annotations. There’s no clear way or documentation to control the order of validation errors or prevent multiple messages for the same field. It would be great if there was official support or guidance on this.

Comment From: alizainofficial25

@wilkinsona Thanks for the suggestion! Hibernate Validator is one way to handle this, but I’m wondering if there could be a way to achieve this with just spring-boot-starter-validation. Spring Boot includes the necessary validation dependencies, but it doesn’t seem to have built-in support for controlling the order of validation errors or preventing multiple messages for the same field. It would be great if this feature could be added natively.

Comment From: philwebb

Have you got a sample that shows the problem? Spring Boot isn't really involved in the actual validation process. We delegate to Spring Framework and that delegates to Hibernate Validator.

Comment From: alizainofficial25

@philwebb Thanks for the reply! I get that Spring Boot delegates validation to Hibernate Validator, but it's frustrating that basic error handling, like controlling validation order or preventing multiple messages for the same field, isn't possible with just spring-boot-starter-validation. It seems like such functionality should be built-in, especially when Hibernate Validator is required just for error handling. but it feels like Spring Boot should handle this better out of the box.

Comment From: alizainofficial25

@philwebb I noticed that the issue was closed as "not planned." Could you please clarify why this decision was made? I strongly believe that handling basic validation errors, such as controlling the error order or preventing multiple messages for the same field, should be a feature provided out-of-the-box in Spring Boot or spring-boot-starter-validation. It would be great if this could be reconsidered or re-opened for further discussion.

Comment From: philwebb

Thanks for the reply! I get that Spring Boot delegates validation to Hibernate Validator, but it's frustrating that basic error handling, like controlling validation order or preventing multiple messages for the same field, isn't possible with just spring-boot-starter-validation

I'm not sure if it's possible or not at the moment because we've not really got to the bottom of your problem. If you can provide a sample that show the issue we may be able to do something.

Could you please clarify why this decision [closed as "not planned"] was made?

We close any issue that heads off to stackoverflow.com as not planned on our side, but that doesn't mean that we can't reopen it or open another issue if necessary. In this case, we're not really what your options are and stackoverflow feels like a good place to get general help. If it turns out that we've got a bug (perhaps we use HashMap and not LinkedHashMap somewhere so we don't get consistent ordering) and that gets discovered during your analysis then we'll treat this one as a bug. If it's possible to do what you want, but it could be easier, then we'll treat it as an enhancement request.

Currently though, we don't really know exactly what you're trying to do, why it's not working and where any such fix/change would be needed (Spring Boot, Spring Framework, Hibernate Validator).