zend-framework2 - Zend Framework 2 FormElementManager 工厂不工作

标签 zend-framework2 zend-form-element

请帮助我使用 Zend 框架 2:)

我想使用表单元素管理器创建一个包含字段集集合的表单(绝对像 in official documentation )。

我的 FormElementManager 配置:

'form_elements' => array(
    'factories' => array(
        'Admin\Form\TaskForm' => function($sm) {
            $form = new TaskForm();
            $doctrimeEntityManager = $sm->getServiceLocator()->get('Doctrine\ORM\EntityManager');
            $form -> setEntityManager($doctrimeEntityManager);
            $form -> init();
            return $form;
        },
        'Admin\Form\TaskbrandFieldset' => function($sm) {
            $doctrimeEntityManager = $sm->get('Doctrine\ORM\EntityManager');
            $form = new TaskbrandFieldset();
            $form->setEntityManager($doctrimeEntityManager);
            return $form;
        },
    )
),

Admin\Form\TaskForm(仅问题部分):

namespace Admin\Form;
use Doctrine\ORM\EntityManager;
use Zend\Form\Form;

class TaskForm extends Form {

protected $entityManager;

public function init() {

    $this->setAttribute('method', 'post');

    // Id
    $this->add(array(
        'name' => 'id',
        'attributes' => array(
            'type' => 'hidden',
        ),
    ));

    // My fieldset
    $this->add(array(
        'type' => 'Zend\Form\Element\Collection',
        'name' => 'taskbrands',
        'options' => array(
            'label' => 'Brand of the product',
            'count' => 0,
            'should_create_template' => true,
            'allow_add' => true,
            'target_element' => array(
                'type'=>'Admin\Form\TaskbrandFieldset'
                ),
        ),
        'attributes' => array(
            'id' => 'addressFieldset'
        )
    ));
}
}

管理\表单\TaskbrandFieldset:

namespace Admin\Form;

use Admin\Entity\Taskbrand;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class TaskbrandFieldset extends Fieldset implements InputFilterProviderInterface, ServiceLocatorAwareInterface {

protected $entityManager;
protected $serviceLocator;

public function init() {
    $this->setName('TaskbrandFieldset');
    $this->setHydrator(new ClassMethodsHydrator(false))
            ->setObject(new Taskbrand());

    $this->setLabel('Taskbrand');

    $this->add(array(
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'name' => 'brand',
        'options' => array(
            'object_manager' => $this->getEntityManager(),
            'target_class' => 'Module\Entity\Brand',
            'property' => 'name',
        ),
    ));

}
}

最后,我的 Controller :

 $Task = $this->getServiceLocator()->get('Admin\Model\Task')->findByPk($id);
 $formManager = $this->getServiceLocator()->get('FormElementManager');
 $form =  $formManager->create('Admin\Form\TaskForm');
 $form->bind($Task);

问题是表单 Admin\Form\TaskForm 在 form_elements 配置部分中描述的工厂中实例化,但 Admin\Form\TaskbrandFieldset 没有。它只是调用。

试图理解这个问题,我发现 Admin\Form\TaskForm 和 Admin\Form\TaskbrandFieldset 使用 FormElementManager 的不同实例进行实例化,第一个里面有我的配置(包括工厂描述),但第二个没有任何内容。

请帮助我:)

最佳答案

问题出在您的 Controller 上。使用

$form = $formManager->get('Admin\Form\TaskForm');

而不是

$form = $formManager->create('Admin\Form\TaskForm');

请记住,您不必使用 $form->init()。它是自动调用的,与 zf1 中相同。 zf2 site 有一个很好的教程

关于zend-framework2 - Zend Framework 2 FormElementManager 工厂不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17648618/

相关文章:

php - 使用 Zend 验证 URL

php - 访问 Zend Framework 2 中的模块配置

zend-framework - Zend Framework 动态添加表单字段并填充

php - Zend 文件字段的错误 - 文件名不会以 zend 形式重新填充

zend-framework - 在 Zend_Form_Element 描述装饰器中使用 HTML

zend-framework - 向表单添加 csrf 是否存在根本性错误?

regex - ZF2 表单验证无法正常用于电子邮件

module - 如何在 Zend Framework 2 中更改模块的 basePath

jquery - 如何向 Zend Form 元素添加 jQuery 事件回调函数

php - 在默认 View 之前加载客户端 View (如果存在)