php - Zend框架2 : Creating forms from configuration - how to fix ServiceNotCreatedException

标签 php forms zend-framework2

我想通过配置在 Zend Framework 2 中创建一个表单,类似于 docs 中所述。 。因此,不要在每个元素的表单类中执行此操作:

// [...]

$this->add(array(
    'name' => 'fname',
    'type' => 'text',     
));

// [...]v

我想这样做:

// [...]

$form = $factory->createForm(array(
    'elements' => array(
        array(
            'spec' => array(
                'name' => 'fname',
                'type' => 'text',
            ),
        ),
    ),
));

// [...]

由于我是这个框架的新手,我仍然有点不确定事情的发展方向以及正确的做事方法是什么,但我认为我走在一条良好的轨道上 - 我只需要让事情正常运转就可以理解他们更多。

目前我的代码导致了一个异常,这让我有点困惑,尽管我以某种方式理解它:

An exception was raised while creating "MyForm"; no instance returned

除了我的困惑之外,我已经知道我的代码可能会在哪里出错。

表单类看起来非常简单,因为实际的元素不会注入(inject)这里:

namespace My\Form;

use Zend\Form\Form;

class MyForm extends Form
{
    public function __construct()
    {
        parent::__construct();

        $this->setAttribute('method', 'post');
        $this->setAttribute('action', '/some-action');
    }
}

表单元素的配置如下所示:

namespace My\Form;

use Zend\Form\Factory;

class MyFormFactory extends Factory
{
    public function __construct()
    {
        $factory = new Factory();

        $form = $factory->createForm(array(
            'elements' => array(
                array(
                    'spec' => array(
                        'name' => 'fname',
                        'type' => 'text',
                    ),
                ),
            ),
        ));

        return $form;
    }
}

为了将两者连接在一起,我在模块的 module.config.php 中设置了此服务管理器配置:

'service_manager' => array(
    'factories' => array(
        'MyForm' => function ($servicemanager) {
            $form = new \My\Form\MyForm();
            $factory = $servicemanager->get('MyFormFactory');
            $form->add($factory);
            return $form;
        },
        'MyFormFactory' => function ($servicemanager) {
            $factory = new \My\Form\MyFormFactory();
            return $factory;
        },
    ),
),

最后但并非最不重要的一点是 Controller :

 namespace My\Controller;


use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class MyController extends AbstractActionController
{
    public function indexAction()
    {
        $form = $this->getServiceLocator()->get('MyForm');

        return new ViewModel(array(
            'form' => $form,
        ));
    }
}

正如已经说过的,唯一发生的事情是实际上非常清楚的异常:MyForm 类不返回实例。为了解决这个问题,我已经尝试返回一些东西,但是没有做任何事情。

需要更改哪些内容才能创建表单?我的方法好还是我走错了路?

最佳答案

为了通过配置创建表单,您需要使用 Zend\Form\Factory 它将根据您提供的配置返回表单元素。我们称之为“FormFactory”。

但是,要使用 FormElementManager 将此表单注册为名为“MyForm”的服务,您还需要创建一个服务管理器工厂。这个“工厂”是一个实现 Zend\ServiceManager\FactoryInterface 的类。我们称之为“ServiceFactory”。

如果我要更正您的代码,我会按以下方式进行。

  • 删除类MyForm。没有真正需要实际扩展基本表单 Zend\Form\Form;这是因为您只是设置表单的属性,可以通过 FormFactory 来完成(您为其提供这些属性的配置,它将返回一个已设置的新表单)。

  • 删除MyFormFactory。为了创建新表单,我们不需要扩展表单工厂或创建它的新实例,而是在客户端代码中使用它的 API。

  • 删除 service_manager 下的配置。我们应该使用“FormElementManager”注册表单(使用“form_elements”配置键)。配置为返回表单元素的“插件管理器”。

通过这些更改,您就可以仅使用一个“ServiceFactory”来创建表单。例如。

namespace My\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MyFormFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $formElementManager)
    {
        // Get the config of the new form
        $config = $this->getElementConfig($formElementManager);

        // Get the service manager
        $serviceManager = $formElementManager->getServiceLocator();

        // Get the form factory (service that creates the form)
        // from the service manager, FormFactory is the default
        $formFactory = new \Zend\Form\Factory($formElementManager); 

        // Create the form using the factory and config and then return it
        return $formFactory->create($config);
    }

    /** Return the config for the factory **/
    protected function getElementConfig(ServiceLocatorInterface $formElementManager)
    {
        // you could use the form element manager / service manager
        // here to load the config from elsewhere...
        return [
            'name' => 'myform',
            'type' => 'Zend\Form\Form',
            'attributes' => [
                'method' => 'post',
                'action' => '/foo/bar',
            ],
            'fieldsets' => [
                /....
            ],
            'elements' => [
               //...
            ]
        ];
    }

}

然后在 module.config.php 中,您可以将工厂注册为新服务​​

'form_elements' => [
    'factories' => [
        'MyForm' => 'My\Factory\MyFormFactory',
    ],
],

注意上面的配置位于“form_elements”键下。当我们使用表单元素管理器注册服务时,您的 Controller 代码也应该更新。

$serviceManager = $this->getServiceLocator();
$form = $serviceManager->get('FormElementManager')->get('MyForm');

关于php - Zend框架2 : Creating forms from configuration - how to fix ServiceNotCreatedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29581322/

相关文章:

php - 从快捷方式 IF 语句在 Blade 模板中渲染 HTML

php - 从android中的mysql数据库中检索edittext中的数据

javascript - 添加到浏览器的内置表单验证

php - 如何在 Zend Framework 2 中引导 session

php - Zend2 Routing __NAMESPACE__ 不起作用或被忽略

php - 使用 PHP 从三个 MySQL 表构建 CSV 表的有效方法

php - CodeIgniter:如果我使用 die() 函数,则不会加载 View

jquery表单在模式中提交并确认

jquery - 修改 DOM 后提交按钮变得无响应

php - 如何(或在哪里)使用 Zend Framework 2 的 EventManager?