Configure FormElementManager
I have a little module that configures the FormElementManager like this:
return[
'form_elements' => [
'factories' => [
PriceFieldset::class => PriceFieldsetFactory::class,
ProductFormInterface::class => ProductFormFactory::class,
],
],
];
This config is loaded by a ConfigProvider class and loaded by the ConfigManager in the /config/config.php file like this:
$configManager = new ConfigManager(
[
Zend\Form\ConfigProvider::class,
Product\ConfigProvider::class,
new PhpFileProvider($pattern),
],
$cachedConfigFile
);
When I want to access the ProductFormInterface::class from the FormElementManager it is not found. I need to these lines to the /config/container.php file to get this running.
$container = new ServiceManager();
(new Config($config['dependencies']))->configureServiceManager($container);
// Inject config
$container->setService('config', $config);
/** @var FormElementManagerV3Polyfill $formElementManager */
$formElementManager = $container->get('FormElementManager');
$formElementManager->configure($config['form_elements']);
return $container;
What am I doing wrong? I don't think this is a wanted behaviour. In my module I also have some validators, filters, hydrators and input filters as well. Currently, I need to add the configuration manually for every specialized service managers.
How do you configure FormElementManager? Do you have a factory that injects it with configuration?
I did something similar here: https://github.com/mtymek/blast-input-filter - I configure validator, filter and input filter plugin managers in a similar manner as you do. Take a look - maybe it will give you some hints. Alternatively, maybe you would like to contribute to my component and add support for form elements?
Like I said, I only have a special ConfigProvider which reads the config file and adds it to the configuration in the /config/config.php. i thought that all the configuration is passed to formElementManager and others. To fix this I added the lines I mentioned.
Your package looks like a better solution though I am missing hydrators and form elements. The name of the package seems a bit misleading when adding hydrators and form elements.
So, basically you're missing a factory that does the code you put in config.php ;) By itself, FormElementManager doesn't know that it should look for form_elements config key.
My package doesn't have hydrators and form elements because I don't use them (well, I use hydrators but I keep them in main SM...). But I'll be happy to accept PR or even consider changing the package name to something more relevant afterwards.
@RalfEggert for this i usually use Delegator Factories like
<?php
namespace My\Namespace\Form;
use Interop\Container\ContainerInterface;
use Zend\Form\FormElementManager\FormElementManagerV3Polyfill;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
class FormElementManagerDelegatorFactory implements DelegatorFactoryInterface
{
public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
{
$formElementManager = $callback();
$config = $container->has('config') ? $container->get('config') : [];
$config = isset($config['form_elements']) ? $config['form_elements'] : [];
(new Config($config))->configureServiceManager($formElementManager);
return $formElementManager;
}
}
and in my ConfigProvider config
<?php
return [
'dependencies' => [
'delegators' => [
'HydratorManager' => [
My\Namespace\Hydrator\HydratorManagerDelegatorFactory::class,
],
'InputFilterManager' => [
My\Namespace\InputFilter\InputFilterManagerDelegatorFactory::class,
],
'FormElementManager' => [
My\Namespace\Form\FormElementManagerDelegatorFactory::class,
],
],
],
];
yes, class naming could be improved ;-)
This repository has been closed and moved to mezzio/mezzio; a new issue has been opened at https://github.com/mezzio/mezzio/issues/16.