2016-08-09 6 views
2

私は、フォームタイプがあります:あなたは私はすでにトランスレータを注入するために私のフォームタイプを設定しようとしていますご覧のようSymfony 3のコントローラ外でTranslatorサービスを使用するにはどうすればよいですか?

<?php 

// src/AppBundle/Form/ProductType.php 
namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Translation\TranslatorInterface; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 

class ProductType extends AbstractType 
{ 

    private $translator; 

    public function __construct(TranslatorInterface $translator) 
    { 
     $this->translator = $translator; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('name', TextType::class) 
      ->add('save', SubmitType::class) 
     ; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Product', 
     )); 
    } 
} 

を。

parameters: 
# parameter_name: value 

services: 
    app.form.product: 
     class: AppBundle\Form\ProductType 
     arguments: ["@translator"] 

しかし、次のエラー受け付けております:私は私のサービスでは

Catchable Fatal Error: Argument 1 passed to AppBundle\Form\ProductType::__construct() must implement interface 

Symfony\Component\Translation\TranslatorInterface, none given, called in /path/to/symfony/bundle/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 85 and defined....

を誰かが与えるものを教えてもらえますか?私はかなりサービスタイプが間違っていると確信していますが、私は私の人生を救うために必要なものを見つけることができません。

答えて

2

フォームのサービス定義に、here in the docのようにform.typeとして正しくタグ付けしているサービスをチェックインします。

news announcementによれば、バージョン2.6から、トランスレータコンポーネントはtranslator.defaultのようなサービスとして定義されています。

services: 
    app.form.product: 
     class: AppBundle\Form\ProductType 
     arguments: ["@translator.default"] 
     tags: 
      - { name: form.type } 

希望このヘルプ

+1

それは働いてからそれを保っていた「タグ」引数の欠如だった:

例として、あなたのようなものを持っている必要があります。また、 "Symfony \ Bundle \ FrameworkBundle \ Translation \ Translatorを使用する"必要がありました。上記の私のコードのようにTranslatorInterfaceの代わりに。バンドルありがとう。またコンソールコマンド "bin/console debug:container"を使用すると、どのサービスが注入可能であるかを知ることができ、使用する "使用"ステートメントを見つけるのに役立ちます。 – pogeybait

関連する問題