in28minutes / master-spring-and-spring-boot

Spring and Spring Boot Tutorial For Absolute Beginners - 10-in-1 - Spring to Spring Boot to REST API to Full Stack to Containers to Cloud
https:/links.in28minutes.com/sb
1.29k stars 648 forks source link

Question about @Valide in Todo project #21

Open sunbyte opened 1 year ago

sunbyte commented 1 year ago

Hello!

I have question about spring boot validation in Todo project.

public String addNewTodo(ModelMap modelMap, @Valid Todo todo, BindingResult bindingResult) {...}

If we change the position of the method parameters and put @Valid Todo todo before ModelMap modelMap, the validation not work correct and we don't see the error message in the form, but getting an exceptions in the browser. Why?

Thanks.

Aayush-Kapur commented 1 year ago

Hey @sunbyte If you're trying to put @Valid Todo todo before ModelMap modelMap , keep in mind that the BindingResult parameter must immediately follow the @Valid annotated object. Spring MVC uses BindingResult to collect validation errors for the todo object. If it's not positioned correctly after @Valid, Spring may not be able to capture validation errors correctly, leading to unexpected behavior.