2017-03-10 14 views
0

フォームイベントリスナーに接続しているユーザーを知る必要があります。だからトークンストレージを私のformTypeクラスに注入し、サービスを宣言しました。しかし、それは動作していないようです。Symfony Injectフォームタイプのトークンストレージ

私は間違っていましたか?

class AlertType extends AbstractType 
{ 
    private $tokenStorage; 

    /** 
    * @param TokenStorageInterface $tokenStorage 
    */ 
    public function __construct(TokenStorageInterface $tokenStorage) 
    { 
     $this->tokenStorage = $tokenStorage; 
    } 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array    $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('email', EmailType::class, array('label' => 'Votre adresse email')); 

     $builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData')); 
    } 

    public function onPreSetData(FormEvent $event) 
    { 
     $user = $this->tokenStorage->getToken()->getUser(); 
     dump($user); 
     die; 
    } 

    /** 
    * @param OptionsResolver $resolver 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'FM\MailAlertBundle\Entity\Alert', 
     )); 
    } 
} 

サービス宣言:

services: 
     fm.mail_alert.form.alert_type: 
      class: FM\MailAlertBundle\Form\AlertType 
      arguments: 
       - '@security.token_storage' 
      tags: 
       - { name: form.type } 

エラーメッセージ:

Type error: Argument 1 passed to FM\MailAlertBundle\Form\AlertType::__construct() must implement interface Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface, none given

私の目的は、ユーザが接続されている場合、フィールドの電子メールを無効にすることです。

+0

どのように使いますか? – Matteo

+0

私の目的は、ユーザーが接続されている場合、フィールドの電子メールを無効にすることです。 @Matteo – Kevin

+0

申し訳ありませんが、あなたはそれを例として使っていますか? '$ form = $ this-> createForm(AlertType :: class、$ alert);'? – Matteo

答えて

0

クール。私はsymfony 2.8を使用していますので、私のコントローラでAlertType::classnew AlertTypeを変更しなければなりませんでした;)

関連する問題