如何使用 zend expressive 配置自定义表单元素

How do I configure a custom form element with zend expressive

在我的 ZF2 应用程序中,我有几个带有注入数据库适配器的自定义表单元素。我用一种方法将配置放在 module.php 文件中,如下所示:

            public function getFormElementConfig()
            {
                return array(
                    'factories' => [
                        'dksCodeElementSelect' => function($container)
                        {
                            $db = $container->get(AdapterInterface::class);
                            $elemnt = new Form\Element\Select\DksCodeElementSelect();
                            $elemnt->setDb($db);
                            return $elemnt;
                        },
                )
            }

如何在具有 zend 表现力的应用程序中配置自定义表单元素?

表单 class 通过 'FormElementManger' 调用元素。 FormElementManager 从配置中读取 'form_elements' 键。它基本上是一个容器(服务管理器),所以你必须像容器一样配置它(工厂、可调用对象、别名等)。此外,您必须将 Zend/Form 模块注册到容器中。我没有尝试过,但必须这样做;

(ps:这里已经太晚了,如果它不起作用请告诉我,这样我就可以举一个有效的例子。)

class MyModule\ConfigProvider {
    public function __invoke()
    {
        return [
            'dependencies' => $this->getDependencies(),
            'templates'    => $this->getTemplates(),
            'form_elements => [
                 /* this is where you will put configuration */
                 'factories' => [
                    MyElement::class => MyElementFactory::class,
                 ]
            ]
       ];
   }
}