3

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

我想使用表单元素管理器创建一个包含字段集集合的表单(绝对像在官方文档中一样)。

我的 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'
        )
    ));
}
}

Admin\Form\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',
        ),
    ));

}
}

最后,我的控制器:

 $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 实例进行实例化,第一个里面有我的配置(包括工厂描述),但第二个什么都没有。

请帮我 :)

4

1 回答 1

0

问题出在您的控制器中。利用

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

代替

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

请记住,您不必使用 $form->init()。它是自动调用的,就像在 zf1 中一样。zf2网站上有一个很好的教程

于 2013-07-17T06:24:43.290 回答