2

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">&times;</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
NayoR
  • 702
  • 6
  • 13
msf6012
  • 119
  • 2
  • 13
  • you might have of problem when transforming the request data into the UserDTO, check if the types are correct (like date or numbers), the method `changeUserFirstName` seems to be ok – Olivier Boissé Apr 29 '18 at 18:10
  • thank you @OlivierBoissé . I was using a UserDto for updates same as the UserDto for registration. However when I was trying to update a field such as firstname, it gives me an error of validations and null pointer because the other fields could not be null entered. My fix was to create one UpdateDto for firstname alone and one UpdateDto for the lastname alone with their specific validations in order to fix this issue. Btw I feel it is not the right technique to have multiple Dto? Is it right to use mutliple Dto? is there a way to pass and change only one value in a Dto model? – msf6012 Apr 29 '18 at 19:30
  • having multiples DTOs for different scenarios is perfectly fine. However it's weird you have one endpoint for updating the firstname and another one for updating the lastname, why not a unique endpoint for updating the user ? – Olivier Boissé Apr 29 '18 at 21:37
  • @OlivierBoissé Because I want the user to be able to update his firstname or lastname separately and I cant doing so in one Dto. because if user tries to update fname using one Dto containing fname and lname, validations and null pointer error will be thrown. – msf6012 Apr 29 '18 at 23:21
  • so why creating a DTO for just one field... you could use `@RequestBody String firstName` instead of `@Valid UserDto userDto` – Olivier Boissé Apr 30 '18 at 00:40

1 Answers1

1

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.

Read also: https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html#isValid-T-javax.validation.ConstraintValidatorContext-

a.k
  • 1,035
  • 10
  • 27
  • Thank you for your reply. Yes I have validations in my UserDto and they worked fine with the registration process. but not with the update user data process with the same UserDto with the same validation. Btw I was entering correct pattern as used to enter in the registration – msf6012 Apr 29 '18 at 16:18