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:
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
services:
ml_user.profile.form.type:
class: Ml\UserBundle\Form\ProfileFormType
arguments: ["@security.context"]
tags:
- { name: form.type, alias: ml_user_profile }
ProfileFormType.php
namespace Ml\UserBundle\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Form\AbstractType;
class ProfileFormType extends AbstractType
{
private $securityContext;
public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;
}
public function buildForm
(FormBuilderInterface
$builder, array $options) {
$user = $this->securityContext->getToken()->getUser();
// Add custom fields
$builder->add('name');
$builder->add('surname');
$builder->add('address');
$builder->add('postCode');
$builder->add('city');
$builder->add('phone');
$builder->add('mobile');
if ($user->getTypeAccount() == 1) {
$builder->add('companyName');
$builder->add('nip');
$builder->add('regon');
}
}
public function getDefaultOptions
(array $options) {
'required' => false,
'data_class' => 'Ml\UserBundle\Entity\User'
);
}
public function getParent()
{
return 'fos_user_profile';
}
public function getName()
{
return 'ml_user_profile';
}
}
W tej nowej kombinuję w ten sposób:
services.yml
services:
ml_user.profile.form.type:
class: Ml\UserBundle\Form\ProfileFormType
arguments: ["@security.token_storage"]
tags:
- { name: form.type, alias: ml_user_profile }
ProfileFormType.php
namespace Ml\UserBundle\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Form\AbstractType;
class ProfileFormType extends AbstractType
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm
(FormBuilderInterface
$builder, array $options) {
$user = $this->get('security.token_storage')->getToken()->getUser();
// Add custom fields
$builder->add('name');
$builder->add('surname');
$builder->add('address');
$builder->add('postCode');
$builder->add('city');
$builder->add('phone');
$builder->add('mobile');
if ($user->getTypeAccount() == 1) {
$builder->add('companyName');
$builder->add('nip');
$builder->add('regon');
}
}
public function getDefaultOptions
(array $options) {
'required' => false,
'data_class' => 'Ml\UserBundle\Entity\User'
);
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\ProfileFormType';
}
public function getBlockPrefix()
{
return 'ml_user_profile';
}
}
Będę wdzięczny za wszelkie wskazówki, które sprawią, że wszystko zacznie działać, jak należy.
Ten post edytował swiezak 27.07.2016, 01:40:59