Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF2][SF][Symfony2] Aktualizacja hasła
damianooo
post 21.06.2021, 21:39:46
Post #1





Grupa: Zarejestrowani
Postów: 493
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Przepisuję swój portal z wersji 2.8 do 4.4 Symfony.
Na wersji 2.8 działała mi dobrze aktualizacja hasła użytkownika natomiast teraz jest z tym jakiś problem.

Mam błąd:

Argument 1 passed to Symfony\Bundle\FrameworkBundle\Controller\AbstractController::createForm() must be of the type string, object given, called in C:\xampp\htdocs\ligatyperow2\src\Controller\MainController.php on line 254

PS. Linia 254 to:
  1. $changePasswdForm = $this->createForm(new ChangePasswordType(), User::class);



Mój kod:


MainController:
  1. public function accountSettingsAction(Request $Request, LoggerInterface $logger)
  2. {
  3. $logger->info('this is the account action');
  4. $User = $this->getUser();
  5.  
  6. // Change Password
  7.  
  8.  
  9. $changePasswdForm = $this->createForm(new ChangePasswordType(), User::class);
  10.  
  11. if($Request->isMethod('POST') && $Request->request->has('changePassword')){
  12. $changePasswdForm->handleRequest($Request);
  13.  
  14. if($changePasswdForm->isValid()){
  15.  
  16. try {
  17. $userManager = $this->get('user_manager');
  18. $userManager->changePassword($User);
  19.  
  20. $this->get('session')->getFlashBag()->add('success', 'Twoje hasło zostało zmienione!');
  21. return $this->redirect($this->generateUrl('liga_typerow_account'));
  22.  
  23. } catch (UserException $ex) {
  24. $this->get('session')->getFlashBag()->add('error', $ex->getMessage());
  25. }
  26.  
  27. }else{
  28. $this->get('session')->getFlashBag()->add('error', 'Popraw błędy formularza2!');
  29. }
  30. }
  31.  
  32.  
  33. return array(
  34. 'user' => $User,
  35. 'changePasswdForm' => $changePasswdForm->createView()
  36. );
  37. }


ChangePasswordType.php :

  1. <?php
  2.  
  3. namespace App\Form;
  4.  
  5. use App\Entity\User;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
  10.  
  11. class ChangePasswordType extends AbstractType {
  12.  
  13. public function getName() {
  14. return 'changePassword';
  15. }
  16.  
  17. public function buildForm(FormBuilderInterface $builder, array $options) {
  18. $builder
  19. ->add('currentPassword', 'password', array(
  20. 'label' => 'Aktualne hasło',
  21. 'mapped' => false,
  22. 'constraints' => array(
  23. new UserPassword(array(
  24. 'message' => 'Podano błędne aktualne hasło użytkownika'
  25. ))
  26. )
  27. ))
  28. ->add('plainPassword', 'repeated', array(
  29. 'type' => 'password',
  30. 'first_options' => array(
  31. 'label' => 'Nowe hasło'
  32. ),
  33. 'second_options' => array(
  34. 'label' => 'Powtórz hasło'
  35. )
  36. ))
  37. ->add('submit', 'submit', array(
  38. 'label' => 'Zmień hasło'
  39. ));
  40. }
  41.  
  42. public function configureOptions(OptionsResolver $resolver)
  43. {
  44. $resolver->setDefaults([
  45. 'data_class' => User::class,
  46. ]);
  47. }
  48.  
  49. }


AccountSettings.html.twig

  1. {% extends 'base.html.twig' %}
  2.  
  3. {% block content %}
  4.  
  5. <div class="div_change_pass_box"></div>
  6. <div class="div_change_pass_box_small_screen"></div>
  7. <div class="div_change_pass">{{ form(changePasswdForm) }}</div>
  8.  
  9. {% endblock %}




Go to the top of the page
+Quote Post
ohm
post 22.06.2021, 07:51:18
Post #2





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

Ostrzeżenie: (0%)
-----


Masz jak byk napisane, ze 1 argument powinien byc stringiem. Szybki rzut oka na tutorial i masz przyklad

  1. $form = $this->createForm(TaskType::class, $task);
Go to the top of the page
+Quote Post
damianooo
post 24.06.2021, 20:56:28
Post #3





Grupa: Zarejestrowani
Postów: 493
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Zmieniłem trochę. I mam teraz błąd:

Unable to guess how to get a Doctrine instance from the request information for parameter "user".


Nie umiem znaleźć błędu.


MainController.php

  1. /**
  2.   * @Route(
  3.   * "/konto",
  4.   * name = "liga_typerow_account"
  5.   * )
  6.   * @Template()
  7.   */
  8. public function accountSettingsAction(Request $request, LoggerInterface $logger, User $user, UserPasswordEncoderInterface $passwordEncoder)
  9. {
  10. $logger->info('this is the account action');
  11.  
  12. $form = $this->createForm(ChangePasswordType::class, $user);
  13. $form->handleRequest($request);
  14.  
  15. $logger->info("@@@ 01");
  16.  
  17. if ($form->isSubmitted() && $form->isValid()) {
  18.  
  19. $logger->info("@@@ 02");
  20.  
  21. // encode the plain password
  22. $user->setPassword(
  23. $passwordEncoder->encodePassword(
  24. $user,
  25. $form->get('password')->getData()
  26. )
  27. );
  28.  
  29. $logger->info("@@@ 03");
  30.  
  31. $entityManager = $this->getDoctrine()->getManager();
  32. $entityManager->persist($user);
  33. $entityManager->flush();
  34.  
  35. $this->addFlash('success', "Password reset successfully");
  36. return $this->redirect($this->generateUrl('liga_typerow_account'));
  37. }
  38.  
  39. return array(
  40. 'user' => $user,
  41. 'form' => $form->createView(),
  42. );
  43. }



ChangePasswordType.php

  1. <?php
  2.  
  3. namespace App\Form;
  4.  
  5. use App\Entity\User;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  8. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11.  
  12. class ChangePasswordType extends AbstractType {
  13.  
  14. public function buildForm(FormBuilderInterface $builder, array $options) {
  15. $builder
  16. ->add('password', RepeatedType::class, array(
  17. 'type' => PasswordType::class,
  18. 'first_options' => array('label' => 'New Password'),
  19. 'second_options' => array('label' => 'Repeat New Password')
  20. ));
  21. }
  22.  
  23. public function configureOptions(OptionsResolver $resolver)
  24. {
  25. $resolver->setDefaults(array(
  26. 'data_class' => User::class
  27. ));
  28. }
  29.  
  30. }



accoutnSettings.html.twig

  1. {% extends 'base.html.twig' %}
  2.  
  3. {% block content %}
  4.  
  5. {{ form_start(form) }}
  6.  
  7. {{ form_label(form.assword.first) }}
  8. {{ form_widget(form.assword.first) }}
  9.  
  10. {{ form_label(form.password.second) }}
  11. {{ form_widget(form.password.second) }}
  12.  
  13. <input type="submit" value="Submit">
  14.  
  15. {{ form_end(form) }}
  16.  
  17. {% endblock %}






OK znalazłem błąd. Miałem tak:


  1. public function accountSettingsAction(Request $request, LoggerInterface $logger, User $user, UserPasswordEncoderInterface $passwordEncoder)
  2. {
  3. $logger->info('this is the account action');
  4.  
  5. $form = $this->createForm(ChangePasswordType::class, $user);


a ma być tak:

  1. public function accountSettingsAction(Request $request, LoggerInterface $logger, UserPasswordEncoderInterface $passwordEncoder)
  2. {
  3. $logger->info('this is the account action');
  4.  
  5. $user = $this->getUser();
  6.  
  7. $form = $this->createForm(ChangePasswordType::class, $user);


a więc nie miałem instancji użytkownika - w sumie to pisało w błędzie.


Ten post edytował damianooo 24.06.2021, 20:29:16
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 26.04.2024 - 15:04