0

在我的 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-expressive 应用程序中配置自定义表单元素?

4

1 回答 1

0

表单类通过“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,
                 ]
            ]
       ];
   }
}
于 2017-10-13T23:32:24.933 回答