Reputation: 129
I am not able to succeed in doing correct way to update a user data like his firstname or lastname
Controller:
@Controller
public class UpdateUserData {
@Autowired
private UserService userService;
@RequestMapping(value = "/user/updateFirstName", method = RequestMethod.POST)
public String changeUserFirstName(RedirectAttributes rm, final Locale locale, @Valid UserDto userDto) {
final User user = (User) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
userService.changeUserFirstName(user, userDto.getFirstName());
rm.addFlashAttribute(
"message",
messages.getMessage("message.updateFirstNameSuc", null, locale)
);
return "redirect:/user/myAccount?lang=" + locale.getLanguage();
}
}
And that is the part related in myAccount html page:
<div class="manageinfo">
<h1>Manage your personal information</h1>
<br/>
<div class="container">
<div th:if="${message != null}" class="alert alert-info"
th:text="${message}">message</div>
<div th:each = "user: ${user}" >
<p>Firstname</p>
<input type="text" th:value="${user.firstName}"><a data-toggle="modal"
data-target="#myModalFirstName"><i class="fa fa-pencil fa-lg"> </i></a>
</div>
<!-- Modal for firstName-->
<div class="modal fade" id="myModalFirstName" role="dialog">
<button type="button" class="close" data-dismiss="modal">×</button>
<form th:action="@{/user/updateFirstName}" th:method="POST">
<p>First Name</p>
<input type="text" name="firstName" required="required">
<span id="firstNameError" style="color:#F41F0E" ></span>
<button type="submit" class="btn btn-default" >Save</button>
</form>
</div>
</div>
</div>
UserServiceImpl:
public void changeUserFirstName(final User user, final String firstname) {
user.setFirstName(firstname);
userRepository.save(user);
}
When I try to change the name I will receive
Failed to resolve argument 2 of type Myproject.dto.UserDto
javax.validation.ValidationException: HV000028: Unexpected exception during isValid call
Upvotes: 2
Views: 3477
Reputation: 1203
The logical update procedure seems to be ok, I would suggest to check the UserDto and it validation annotation (@NotNull, @Size, etc..) over it fields, as isValid is thrown by @Valid annotation of your'e controller as data did not follow the given constraints.
E.g, Your UserDTO might contain:
@Size(max = 30)
private String firstName;
@Pattern(regexp = "....")
private String lastName;
But your'e controller have recieved a firstName with size larger than maximum / firstName with a wrong pattern.
Upvotes: 1