我需要将数据从Controller传递到Fieldset,如果我使用serviceLocator和FactoryInterface来获取表单,我该怎么做?它甚至可能吗?
目前我的文件如下所示:
控制器
$eventID = $id;
$matuserID = $this->zfcUserAuthentication()->getIdentity()->getId();
// DATA I would like to pass to the form
$dataDB = array('eventid' => $eventID, 'matuserid' => $matuserID);
// Get Form
$form = $this->getServiceLocator()->get('mat_multimail_createform');
CreateFormFactory
public function createService(ServiceLocatorInterface $sm)
{
$multimailFieldset = $sm->get('mat_multimail_fieldset');
$form = new MultiMailCreateForm($multimailFieldset);
// Set the hydrator
$form->setHydrator(new DoctrineHydrator($sm->get('Doctrine\ORM\EntityManager')));
return $form;
}
的CreateForm
public function __construct(FieldsetInterface $multimailFieldset)
{
parent::__construct('create-multimail');
//set the base fieldset
$multimailFieldset->setUseAsBaseFieldset(true);
$this->add($multimailFieldset);
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Speichern',
'class' => 'btn btn-primary',
'id' => 'submultimail'
)
));
}
CreateFieldsetFactory
public function createService(ServiceLocatorInterface $sm)
{
$om = $sm->get('Doctrine\ORM\EntityManager');
$form = new MultiMailFieldset($om, $options);
// Set the hydrator
$form->setHydrator(new DoctrineHydrator($om));
$form->setObject(new Event());
return $form;
}
字段集
public function __construct(ObjectManager $om, $options = array())
{
parent::__construct('multimail');
$this->add(array(
'type' => 'Zend\Form\Element\Hidden',
'name' => 'id'
));
$this->add(array(
'type' => 'Zend\Form\Element\Hidden',
'name' => 'eventid'
));
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'extadressen',
'attributes' => array(
'class' => 'form-control',
'id' => 'singleExtSel',
'multiple' => 'multiple',
),
'options' => array(
'object_manager' => $om,
'target_class' => 'Mat\Entity\AdressenExt',
'property' => 'email',
'is_method' => true,
'find_method' => array(
'name' => 'getExtAdressen',
'params' => array(
'criteria' => array('userid' => $options['matuserid'], 'eventid' => $options['eventid']),
),
),
),
));
或者我必须将控制器更改为:
$options = array('eventid' => 1, 'matuserid' => 2);
$form = new MultiMailFieldset($om, $options);
// Set the hydrator
$form->setHydrator(new DoctrineHydrator($om));
$form->setObject(new Event());
谢谢!
答案 0 :(得分:0)
好的,我想通了,感谢这篇文章ZF2 Get params in factory! 我在工厂拿到了参数并将它们传递给了字段集:
FieldsetFactory
public function createService(ServiceLocatorInterface $sm)
{
$om = $sm->get('Doctrine\ORM\EntityManager');
// add user id
$authUser = $sm->get('zfcuser_auth_service');
$authUserId = $authUser->getIdentity()->getId();
// add current eventID to fetch addresses
$router = $sm->get('router');
$request = $sm->get('request');
$routerMatch = $router->match($request);
$eventID = $routerMatch->getParam("id");
$options = array('userid' => $authUserId, 'eventid' => $eventID);
$form = new MultiMailFieldset($om, $options);
// Set the hydrator
$form->setHydrator(new DoctrineHydrator($om));
$form->setObject(new Event());
return $form;
}
字段集
public function __construct(ObjectManager $om, $options = array())
{
parent::__construct('multimail');
$this->add(array(
'type' => 'Zend\Form\Element\Hidden',
'name' => 'id'
));
$this->add(array(
'type' => 'Zend\Form\Element\Hidden',
'name' => 'eventid'
));
// add current eventID to fetch addresses
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'extadressen',
'attributes' => array(
'class' => 'form-control',
'id' => 'singleExtSel',
'multiple' => 'multiple',
),
'options' => array(
'object_manager' => $om,
'target_class' => 'Mat\Entity\AdressenExt',
'property' => 'email',
'is_method' => true,
'find_method' => array(
'name' => 'getExtAdressen',
'params' => array(
'criteria' => array('userid' => $options['userid'], 'eventid' => $options['eventid']),
),
),
),
)); ...