2013-08-08 17 views

答えて

4

コンストラクタで必要なサービスをフォームタイプに挿入するだけです。

+2

また、サービスファイルのFormType宣言に新しい引数を追加する必要があります。この回答を完全に機能させるには、services.yml – dxvargas

+0

のフォームタイプを[サービスとして定義]する必要があります(http://symfony.com/doc/current/form/create_custom_field_type.html#creating-your-field-サービスとしてのタイプ) – ShinDarth

3

フォームタイプをサービスとして宣言する方法については、this page in the sympfony docsを参照してください。そのページには、多くの優れたドキュメントと例があります。

Cyprianは適切なトラックにありますが、リンクされたページには、フォームタイプをサービスとして作成し、DIコンテナにサービスを自動的に挿入させることでさらにステップが入ります。前回の回答/コメントに基づいて完全な答えとして

5

あなたのフォームタイプからサービスにアクセスするためには、あなたがする必要があります:

1)Define your Form Type as a serviceとそれに必要なサービスを挿入します:

// src/AppBundle/Form/MyFormType.php 
class MyFormType extends AbstractType 
{ 
    protected $myService; 

    public function __construct(MyServiceClass $myService) 
    { 
     $this->myService = $myService; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $this->myService->someMethod(); 
     // ... 
    } 
} 

# src/AppBundle/Resources/config/services.yml 
services: 
    app.my.form.type: 
     class: AppBundle\Form\MyFormType # this is your form type class 
     arguments: 
      - '@my.service' # this is the ID of the service you want to inject 
     tags: 
      - { name: form.type } 

2)今すぐあなたのフォームタイプのクラスで、コンストラクタに注入3210