Guest User

Untitled

a guest
Jun 6th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. public function process(UserInterface $user)
  2. {
  3. $this->form->setData($user);
  4.  
  5. if ('POST' === $this->request->getMethod()) {
  6.  
  7. $password = trim($this->request->get('fos_user_profile_form')['password']) ;
  8. // Checked where password is empty
  9. // But when I remove the password field, it doesn't update anything.
  10. if(empty($password))
  11. {
  12. $this->form->remove('password');
  13. }
  14.  
  15. $this->form->bind($this->request);
  16.  
  17. if ($this->form->isValid()) {
  18. $this->onSuccess($user);
  19.  
  20. return true;
  21. }
  22.  
  23. // Reloads the user to reset its username. This is needed when the
  24. // username or password have been changed to avoid issues with the
  25. // security layer.
  26. $this->userManager->reloadUser($user);
  27. }
  28.  
  29. $form = $this->createFormBuilder()
  30. ->add('name', 'text')
  31. ->add('email', 'repeated', array('type' => 'email'))
  32. ->add('password', 'repeated', array('type' => 'password', 'mapped' => false))
  33. // ...
  34. ->getForm();
  35.  
  36. // Symfony 2.3+
  37. $form->handleRequest($request);
  38.  
  39. // Symfony < 2.3
  40. if ('POST' === $request->getMethod()) {
  41. $form->bind($request);
  42. }
  43.  
  44. // all versions
  45. if ($form->isValid()) {
  46. $user = $form->getData();
  47.  
  48. if (null !== $form->get('password')->getData()) {
  49. $user->setPassword($form->get('password')->getData());
  50. }
  51.  
  52. // persist $user
  53. }
  54.  
  55. $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormInterface $form) {
  56. $form = $event->getForm();
  57. $user = $form->getData();
  58.  
  59. if (null !== $form->get('password')->getData()) {
  60. $user->setPassword($form->get('password')->getData());
  61. }
  62. });
  63.  
  64. /my/Entity/User
  65.  
  66. public function setPassword($password)
  67. {
  68. if ($password) {
  69. $this->password = $password;
  70. }
  71. }
Add Comment
Please, Sign In to add comment