Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF][SF3.1] Nadpisanie formularza służącego do edycji profilu usera
Forum PHP.pl > Forum > PHP > Frameworki
swiezak
Witam.
Do tej pory korzystałem z frameworka Symfony opatrzonego numerkiem 2.5 i security.context, aby pobrać typ użytkownika (getTypeAccount) oraz wyświetlać wybrane pola w formularzu edycji profilu. W SF 3.1 zrezygnowano z security.context. Niby pierdółka, a nie mogę przerobić formsa na "nowszą wersję". Rzuci ktoś z Was okiem i podpowie, gdzie robię błąd?
Zwrotka z błędem jest następująca:
  1. Argument 1 passed to Ml\UserBundle\Form\ProfileFormType::__construct() must be an instance of Ml\UserBundle\Form\TokenStorageInterface, instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage given.



Na początek stara wersja, która działała:

services.yml
  1. services:
  2. ml_user.profile.form.type:
  3. class: Ml\UserBundle\Form\ProfileFormType
  4. arguments: ["@security.context"]
  5. tags:
  6. - { name: form.type, alias: ml_user_profile }


ProfileFormType.php
  1. namespace Ml\UserBundle\Form;
  2.  
  3. use Symfony\Component\Form\FormBuilderInterface;
  4. use Symfony\Component\Security\Core\SecurityContext;
  5. use Symfony\Component\Form\AbstractType;
  6.  
  7. class ProfileFormType extends AbstractType
  8. {
  9. private $securityContext;
  10.  
  11. public function __construct(SecurityContext $securityContext)
  12. {
  13. $this->securityContext = $securityContext;
  14. }
  15.  
  16. public function buildForm(FormBuilderInterface $builder, array $options)
  17. {
  18. $user = $this->securityContext->getToken()->getUser();
  19.  
  20. // Add custom fields
  21. $builder->add('name');
  22. $builder->add('surname');
  23. $builder->add('address');
  24. $builder->add('postCode');
  25. $builder->add('city');
  26. $builder->add('phone');
  27. $builder->add('mobile');
  28.  
  29. if ($user->getTypeAccount() == 1) {
  30. $builder->add('companyName');
  31. $builder->add('nip');
  32. $builder->add('regon');
  33. }
  34. }
  35.  
  36. public function getDefaultOptions(array $options)
  37. {
  38. return array(
  39. 'required' => false,
  40. 'data_class' => 'Ml\UserBundle\Entity\User'
  41. );
  42. }
  43.  
  44. public function getParent()
  45. {
  46. return 'fos_user_profile';
  47. }
  48.  
  49. public function getName()
  50. {
  51. return 'ml_user_profile';
  52. }
  53. }


W tej nowej kombinuję w ten sposób:

services.yml
  1. services:
  2. ml_user.profile.form.type:
  3. class: Ml\UserBundle\Form\ProfileFormType
  4. arguments: ["@security.token_storage"]
  5. tags:
  6. - { name: form.type, alias: ml_user_profile }


ProfileFormType.php
  1. namespace Ml\UserBundle\Form;
  2.  
  3. use Symfony\Component\Form\FormBuilderInterface;
  4. use Symfony\Component\Security\Core\Security;
  5. use Symfony\Component\Form\AbstractType;
  6.  
  7. class ProfileFormType extends AbstractType
  8. {
  9. private $tokenStorage;
  10.  
  11. public function __construct(TokenStorageInterface $tokenStorage)
  12. {
  13. $this->tokenStorage = $tokenStorage;
  14. }
  15.  
  16. public function buildForm(FormBuilderInterface $builder, array $options)
  17. {
  18. $user = $this->get('security.token_storage')->getToken()->getUser();
  19.  
  20. // Add custom fields
  21. $builder->add('name');
  22. $builder->add('surname');
  23. $builder->add('address');
  24. $builder->add('postCode');
  25. $builder->add('city');
  26. $builder->add('phone');
  27. $builder->add('mobile');
  28.  
  29. if ($user->getTypeAccount() == 1) {
  30. $builder->add('companyName');
  31. $builder->add('nip');
  32. $builder->add('regon');
  33. }
  34. }
  35.  
  36. public function getDefaultOptions(array $options)
  37. {
  38. return array(
  39. 'required' => false,
  40. 'data_class' => 'Ml\UserBundle\Entity\User'
  41. );
  42. }
  43.  
  44. public function getParent()
  45. {
  46. return 'FOS\UserBundle\Form\Type\ProfileFormType';
  47. }
  48.  
  49. public function getBlockPrefix()
  50. {
  51. return 'ml_user_profile';
  52. }
  53. }



Będę wdzięczny za wszelkie wskazówki, które sprawią, że wszystko zacznie działać, jak należy.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.