1

I just update my ZF2 to V2.1.4 so that I can use the init method of the Zend\Form class for inject the ServiceLocator into FieldSet as explains the Zend documentation.

I have in the Global.php and Module.php files a definitions of Database driver and a service ,how also explains the documentation. This service works ok in all project.

This is my Fieldset Code:

    class UserFieldset extends Fieldset implements ServiceLocatorAwareInterface{
        protected $serviceLocator;  
            public function setServiceLocator(ServiceLocatorInterface $sl) {
                $this->serviceLocator = $sl;
            }
            public function getServiceLocator() {
                return $this->serviceLocator;
            }   
            public function init() {
                var_dump($this->serviceLocator);
            }

            public function __construct() {
                    .........
            }

}

My problem is the follow:

Inside of UserFieldset::init() method, $this->serviceLocator isn't instance of Zend\ServiceManager\ServiceManager like when get the service from controller. The instance is of Zend\Form\FormElementManager and using var_dump, see that it doesn't have any service of my Zend configuration.

How can I have a Zend\ServiceManager\ServiceManager instance inside the Fieldset using DI?

Please help me.

josepmra
  • 617
  • 9
  • 25

3 Answers3

2

You can grab the service manager instace like this:

public function init() {
    // You will get the application wide service manager
    $sm = $this->getFormFactory()->getFormElementManager()->getServiceLocator();

    // Now you can get things like the application configuration
    $config = $sm->get('Config');
}

Note: Make sure your fieldset is created via the FormElementManager.

Josias Iquabius
  • 1,219
  • 11
  • 11
  • 1
    I'm using a fieldset as a form collection type, and this doesn't work for me; it says: Call to a member function get() on null. – Aise Jan 31 '16 at 18:22
0

You pretty much have to constructor-Inject all your stuff. I do it like the following:

$form = new SomeForm($serviceManager)

Inside SomeForm:

public function __construct(ServiceManager $serviceManager) {
    $fieldSet = new SomeFieldset($serviceManager)

    $this->add($fieldSet);
}

You can nest this basically as deep as you wish to. A good Documentation of how this is all done is

Sam
  • 16,435
  • 6
  • 55
  • 89
  • This solution already was contemplated, I would like inject without use this tecnique (as show Zend documentation version 2.1). – josepmra Mar 29 '13 at 19:57
  • 1
    @josepmra Are you calling the form itself form the ServiceManager? The Interface is there because probably somewhere in ZF there's an initializer that will call the getter after construction. But you need to call the Form from the ServiceManager! Then the injection will take place. – Sam Mar 29 '13 at 21:05
0

Try calling the FormElementManagers getServiceLocator() method

public function init() {
    var_dump($this->serviceLocator->getServiceLocator());
}
Crisp
  • 11,417
  • 3
  • 38
  • 41