title: Getting objectManager / serviceLocator in fieldset in ZF2
body: <p>In order to get my object manager inside my fieldset's init() function I followed the <a href="http://framework.zend.com/manual/2.1/en/modules/zend.form.advanced-use-of-forms.html#the-specific-case-of-initializers" rel="nofollow">docs</a>
  At first I found out that I had to change</p>
  
  <pre><code>public function setServiceLocator(ServiceLocator $sl)
  </code></pre>
  
  <p>to</p>
  
  <pre><code>public function setServiceLocator(ServiceLocatorInterface $sl)
  </code></pre>
  
  <p>otherwise I got an error:</p>
  
  <pre><code>setServiceLocator() must be compatible with that of Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator()
  </code></pre>
  
  <p>When calling <code>$this-&gt;getServiceLocator()</code> I get an instance of the FormManager.
  Additionally calling <code>$this-&gt;getServiceLocator()-&gt;getServiceLocator()</code> returns <code>NULL</code>.</p>
  
  <p>Since I am still new to DI I wonder if I am missing a place to inject?</p>
  
  <p>Testing I switched from</p>
  
  <pre><code>$form = new MyForm();
  </code></pre>
  
  <p>to</p>
  
  <pre><code>$formManager = $this-&gt;serviceLocator-&gt;get('FormElementManager');
  $form        = $formManager-&gt;get('Application\Form\MyForm');
  </code></pre>
  
  <p>Since then I get this error:</p>
  
  <pre><code>exception 'Zend\Di\Exception\RuntimeException' with message 'Invalid instantiator of type "NULL" for "Zend\ServiceManager\ServiceLocatorInterface".'
  An abstract factory could not create an instance of applicationformmyform(alias: Application\Form\MyForm).
  </code></pre>
  
  <p>Anyway reading some other threads using the <code>ServiceLocator</code> Awareness isn't recommended. Is using the <code>FormElementProviderInterface</code> the alternative?</p>
  
  <p>I used the ServiceLocatorAwareInterface before in my classes like this:</p>
  
  <pre><code>use Zend\ServiceManager\ServiceLocatorAwareInterface;
  use Zend\ServiceManager\ServiceLocatorInterface;
  
  class MyClass implements ServiceLocatorAwareInterface
  {
      protected $services;
  
      public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
      {
          $this-&gt;services = $serviceLocator;
      }
  
      public function getServiceLocator()
      {
          return $this-&gt;services;
      }
  </code></pre>
  
  <p>and simply calling it per service locator in my c</p>
  
  <pre><code>$sm = $this-&gt;getServiceLocator();
  $myClass = $sm-&gt;get('Application\Service\MyClass');
  </code></pre>
  
  <p>without having to set an DI. Is this necessary for Fieldsets / Form and where / how exactely?</p>
  
  <p>I tried to inject my Form and Fieldset this way:</p>
  
  <pre><code>    'service_manager' =&gt; array(
      'factories' =&gt; array(
          'Application\Form\CreateCostcenter' =&gt; function (\Zend\ServiceManager\ServiceLocatorInterface $sl) {
              $form = new \Application\Form\CreateCostcenter();
              $form-&gt;setServiceLocator($sl);
              return $form;
          },
          'Application\Form\CostcenterFieldset' =&gt; function (\Zend\ServiceManager\ServiceLocatorInterface $sl) {
              $fieldset = new \Application\Form\CostcenterFieldset();
              $fieldset-&gt;setServiceLocator($sl);
              return $fieldset;
          },
      ),
  ),
  </code></pre>
  
  <p>The injection works for my form when calling it in my controller:</p>
  
  <pre><code>$form = $this-&gt;getServiceLocator()-&gt;get('Application\Form\CreateCostcenter');
  </code></pre>
  
  <p>But of course it won't pass the serviceManager to the Fieldset.</p>
  
  <p>Anyway I don't understand why there has to be a config for the serviceManagerAwareness since it works with other class by just implementing it.
  There also is no hint in the docs for advanced usage since <strong>ZF 2.1</strong>:</p>
  
  <p><a href="http://framework.zend.com/manual/2.1/en/modules/zend.form.advanced-use-of-forms.html#the-specific-case-of-initializers" rel="nofollow">use an initializer (like Zend\ServiceManager\ServiceLocatorAwareInterface) to inject a specific object to all your forms/fieldsets/elements</a></p>