Just got a strange behavior with validation of a very simple HTML form with Spring MVC and Thymleaf.
I have this methods for edit page rendering and submitted form handle.
@GetMapping("/{id}")
public String editPage(@PathVariable("id") Long id, Model model) {
model.addAttribute("user", userService.findById(id)
.orElseThrow(NotFoundException::new));
return "user_form";
}
@PostMapping("/update")
public String update(@Valid UserRepr user, BindingResult result, Model model) {
logger.info("Update endpoint requested");
if (result.hasErrors()) {
return "user_form";
}
userService.save(user);
return "redirect:/user";
}
The wired thing in the update() method is that in case of a validation error the attribute of the model with form content has the name "userRepr" but not "user" as I expect and the thymleaf form view failed because of that.
It's easy to fix the problem by renaming the attribute but Is there some contract about such attribute naming? Is it changeable?