Zf2 - I am not able to define the form as shared one using its name as well as alias

131 views Asked by At

I am working on ZendFramework 2. I have a form which I want to be used as a shared instance. But the shared key only accepts the actual class rather then the name allocated to it. Sharing some of the code snippet for better understanding of my problem:

SampleForm.php

namespace MyProject\Form;

use Zend\Form\Form;

class Sampleform extends Form
{
    public function __construct()
    {
        parent::__construct('sampelname');
    }

    /**
     * Initialize the form elements
     */
    public function init()
    {
        $this->add(
            [
                'type' => 'Text',
                'name' => 'name',
                'options' => [
                    'label' => 'Enter your name',
                ]
            ]
        );
    }
}

Defining the SampleForm in Module.php:

namespace MyProject;

use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\FormElementProviderInterface;
use MyProject\Form\SampleForm;

class Module implements ConfigProviderInterface, FormElementProviderInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    /**
     * {@inheritdoc}
     */
    public function getFormElementConfig()
    {
        return [
            'invokables' => [
                'MyProject\Form\SharedSampleForm' => SampleForm::class,
            ],
            'aliases' => [
                'sharedSampleForm' => 'MyProject\Form\SharedSampleForm'
            ],
            'shared' => [
                'MyProject\Form\SharedSampleForm' => true
            ]
        ];
    }
}

It throws me the error like: Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\Form\FormElementManager\FormElementManagerV2Polyfill::setShared: A service by the name "MyProject\Form\SharedSampleForm" was not found and could not be marked as shared

But it works as expected when I define my getFormElementConfig in Module.php as follows:

public function getFormElementConfig()
{
    return [
        'invokables' => [
            'MyProject\Form\SharedSampleForm' => SampleForm::class,
        ],
        'aliases' => [
            'sharedSampleForm' => 'MyProject\Form\SharedSampleForm'
        ],
        'shared' => [
            SampleForm::class => true
        ]
    ];
}

i.e. In the shared key I provide the reference to the actual Form class name.

If the same definitions are defined under getServiceConfig() then it works as expected without throwing any such error.

Can some one please suggest/help me out how can I be able to use the service name in the shared for forms then providing the actual class reference?

1

There are 1 answers

8
Dolly Aswin On

getFormElementConfig() is used for defining Form Element. Not used for defining Form as service. If you wanna define this form as Service, you should define it under getServiceConfig().

Another tips, if you have make an alias, just define the Service name using it's class name.

public function getServiceConfig()
{
    return [
        'invokables' => [
            SampleForm::class => SampleForm::class,
        ],
        'aliases' => [
            'sharedSampleForm' => SampleForm::class
        ],
        'shared' => [
            SampleForm::class => true
        ]
    ];
}

You can call the Form using the alias name like this $this->getServiceLocator()->get('sharedSampleForm');