3
請幫我與Zend框架2 :)Zend框架2 FormElementManager工廠不能正常工作
我想創建一個使用窗體元素管理器(絕對喜歡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;
},
)
),
聯繫\表格\ 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',
),
));
}
}
最後,我的控制器:
$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的不同實例,第一個有我的配置裏面(包括工廠描述),但其次沒有。
請幫我:)
表單元素**管理器是創建表單**元素**)使用正常的服務工廠創建您的表單(service_manager =>工廠) – Sam
我試過了。如果使用正常服務工廠(在控制器$ this-> getServiceLocator() - > get('Admin \ Form \ TaskForm'))TaskForm實例化正確,但Admin \ Form \ TaskbrandFieldset仍然使用FormElementManager實例化,沒有配置。在[官方文檔](http://framework.zend.com/manual/2.2/en/modules/zend.form.advanced-use-of-forms.html#handling-dependencies)中,他們使用FormElementManager。 – user2582502
嗯,我個人喜歡在[DoctrineDocs](https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md#a-complete-example-using-zendform)中指出並簡單地在我已經知道EntityManager的表單中實例化Fieldset,因此很容易傳遞它。 – Sam