2013-08-08 7 views

उत्तर

4

बस प्रपत्र प्रकार के निर्माता के माध्यम से सेवाओं आप चाहते हैं इंजेक्षन। कैसे एक सेवा के रूप में अपने प्रपत्र प्रकार की घोषणा करने के वर्णन के लिए this page in the sympfony docs पर

class FooType extends AbstractType 
{ 
    protected $barService; 

    public function __construct(BarService $barService) 
    { 
     $this->barService = $barService; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $this->barService->doSomething(); 
     // (...) 
    } 
} 
+2

आपको सेवाओं फ़ाइल में फॉर्मटाइप घोषणा में नया तर्क भी जोड़ना होगा, उदा। इस उत्तर को पूरी तरह से काम करने के लिए services.yml – dxvargas

+0

में, आपके फॉर्म प्रकार को [सेवा के रूप में परिभाषित किया जाना चाहिए] (http://symfony.com/doc/current/form/create_custom_field_type.html#creating-your-field- टाइप-के रूप में एक सेवा) – ShinDarth

3

देखो। उस पृष्ठ में बहुत अच्छा दस्तावेज और उदाहरण है।

साइप्रायन सही रास्ते पर है, लेकिन लिंक किए गए पृष्ठ एक सेवा के रूप में अपने प्रपत्र प्रकार बनाने और डि कंटेनर स्वचालित रूप से सेवा इंजेक्षन होने से एक कदम आगे यह लेता है।

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) अब आप अपने प्रपत्र प्रकार कक्षा में, यह निर्माता में इंजेक्षन

संबंधित मुद्दे