BindingResult from RedirectAttributes flashAttribute ignored , cant send my errors to thymeleaf
I’m new in spring framework and have some problems.I have no idea why my errors cannot be passed with redirect attributes to thymeleaf.
I want to validate my form input in Thymeleaf but I can’t see error messages in my view. I can see every message from BindingResults in debugger mode by I can’t figure out why I can’t see it in my template.
Here are my Model and Controller:
Model —>
public class UserAddBindingModel { @NotBlank(message = "Username is required.") private String username; @NotBlank(message = "First Name is required.") private String firstName; @NotBlank(message = "Last Name is required.") private String lastName; public UserAddBindingModel() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
Controller —->
@GetMapping("register") public ModelAndView registerUser(ModelAndView modelAndView,RedirectAttributes redirectAttributes, @ModelAttribute("userAddBindingModel") UserAddBindingModel userAddBindingModel) { modelAndView.setViewName("auth-register"); if (modelAndView.getModel().containsKey("errors")) { System.out.println(); } return modelAndView; } @PostMapping("register") public ModelAndView registerUserConfirm(ModelAndView modelAndView, @ModelAttribute("userAddBindingModel") @Valid UserAddBindingModel userAddBindingModel, BindingResult bindingResult, RedirectAttributes redirectAttributes) { if (bindingResult.hasErrors()) { modelAndView.setViewName("redirect:register"); redirectAttributes.addFlashAttribute("userAddBindingModel",userAddBindingModel); redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.userAddBindingModel",bindingResult); // modelAndView.addObject("errors",bindingResult.getAllErrors()); } else { modelAndView.setViewName("redirect:/"); this.userService.registerUser(this.modelMapper.map(userAddBindingModel, UserServiceModel.class)); } return modelAndView; }
And finally the view ->
<form th:object="${userAddBindingModel}" action="/users/register" method="POST" class="main-form mx-auto col-md-8 d-flex flex-column justify-content-center"> <div class="row"> <div class="form-group col-md-6 mb-3"> <label for="firstName" class="text-white font-weight-bold">First Name</label> <input th:field="*{firstName}" id="firstName" type="text" min="2" max="20" class="form-control" placeholder="First name" th:errorclass="field-error" /> <p th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}" class="errors alert alert-danger"> First name is required. </p> </div> <div class="form-group col-md-6 mb-3"> <label for="lastName" class="text-white font-weight-bold">Last Name</label> <input th:field="*{lastName}" id="lastName" type="text" min="2" max="20" class="form-control" placeholder="Last name" /> <p class="errors alert alert-danger"> Last name is required. </p> </div> </form>
it does not display any error.Am I missing something? Thanks in advance :).