2
votes

In my Zend\Form\Fieldset AddressFieldset it needs a Zend\Db\TableGateway\AbstractTableGateway BundeslandTable for a \Zend\Form\Element\Select().

So i implement \Zend\ServiceManager\ServiceManagerAwareInterface in this AddressFieldset and use the init() instead __construct().

And in module.config.php (not only in 'form_elements' tested, also in 'service_manager')

'form_elements' => array(
    'factories' => array(
        'MyFormway\Form\Fieldset\Address' => function($sm) {
            $addressFieldset = new MyFormway\Form\Fieldset\AddressFieldset();
            $addressFieldset->setServiceManager($sm);
            return $addressFieldset;
        }
    ),
),

In a \Zend\Form\Form's init():

$this->add(array(
        'type' => 'MyFormway\Form\Fieldset\Address',
        'name' => 'address',
    ));

this throws an error:

Zend\Form\FormElementManager::get was unable to fetch or create an instance for MyFormway\Form\Fieldset\Address

Why is zend unable to fetch an instance of this Fieldset?

edit-----------------------

'form_elements' => array(
    'factories' => array(
        'MyFormway\Form\Fieldset\Address' => function($formElementManager) {
            die('inna form_elements config');
            $addressFieldset = new \MyFormway\Form\Fieldset\AddressFieldset();
            $addressFieldset->setServiceManager($formElementManager->getServiceLocator());
            return $addressFieldset;
        }
    ),
),

Because i have the Zend\Form\FormElementManager i fetch the ServiceLocator ...perhaps dont needed, because all XxxManager extends the Zend\ServiceManager\AbstractPluginManager and this extends ServiceManager. In FormElementManager and also in AbstractPluginManager are no method getServiceManager().

But my problem: the die() is not called plus the error above. Is it a bug? ...i stand for a big wall :(

edit-----------------------

It works for a Form but not for a Fieldset!!!

1

1 Answers

0
votes

Can you do a quick check if the \Invokable is called at all? Some professional die()-debugging will suffice.

Other than that a potential error source would be your injection of the ServiceManager. In the code you provide you're not actually injecting the ServiceLocator but rather the FormElementManager.

$addressFieldset->setServiceManager($sm->getServiceManager());

Doing it this way is considered Bad-Practice tho. You should only inject the stuff that you actually do need. Given you're injecting the whole manager i assume you're either working with Doctrine or you'll need access to some DB-Data. Do it like this:

'Foo' => function ($formElementManager) {
    $sl = $formElementManager->getServiceManager();
    $fs = new FooFieldset();
    $fs->setDbDependency(
        $sl->get('MyDbDependency')
    );
    return $fs;
}

Last little note: when you're adding a Fieldset, you don't need to add 'name' => 'foo' within the $this->add(), since the name of the fieldset will be defined via the Fieldset __construct('name').