phpzend-formzend-framework3zend-servicemanager

Call Service With Options In Zend Framework 3


In ZF3, I am calling a form factory from a controller using this notation:

    $form = $this->formManager->get(myForm::class);

not

    $form = new myForm();

In the factory, I'm using what ZF3 recommends for the method:

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
       //...
    }

I assume that the $options array is intended for passing parameters to the function. How do I populate the $options array in the controller?


Solution

  • I think FormManager above also child of ServiceManager. So, instead of using get() like this

    $form = $this->formManager->get(myForm::class);
    

    I think you can use build(). Here the example

    $form = $this->formManager->build(myForm::class, $options);
    

    And the options should be passed in the form factory

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        return MyForm($options);
    }