I am trying to get the current user in a Custom Form Field Type.
My formType
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class UserType extends AbstractType {
protected $doctrine;
protected $tokenStorage;
public function __construct($doctrine,TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
$this->doctrine = $doctrine;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$user = $this->tokenStorage->getToken()->getUser();
$builder
->setAction($options['data']['url'])
->setMethod('GET')
->add('userType', 'choice', array('choices' => array(
'userType_p' => $pId,
'userType_t' => $tId),
'choices_as_values' => true, 'label' => 'Usertype ',
'expanded' => true, 'multiple' => true,
'translation_domain' => 'User',))........
....
Here is my service:
user.form.token:
class: UserBundle\Form\UserType
arguments: ['@security.token_storage']
tags:
- { name: form.type }
In the Controller I am calling the form like this:
$form = $this->createForm(new UserType($em,$this->get('user.form.token')), $data....
I am getting this following error:
Catchable Fatal Error: Argument 2 passed to UserBundle\Form\UserType::__construct() must implement interface Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface, none given, called in......