【问题标题】:How to pass data in ZF2 from Controller to Form using serviceLocator如何使用 serviceLocator 将 ZF2 中的数据从 Controller 传递到 Form
【发布时间】:2015-01-14 00:20:24
【问题描述】:

我需要将数据从控制器传递到字段集,如果我使用 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');

创建表单工厂

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;
}

创建表单

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

创建字段集工厂

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());

谢谢!

【问题讨论】:

    标签: php forms controller zend-framework2


    【解决方案1】:

    好的,我想通了,感谢这篇文章ZF2 Get params in factory! 我在工厂中获取参数并将它们传递给字段集:

    字段集工厂

    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']),
                    ),
                ),
            ),
        )); ...
    

    【讨论】:

      猜你喜欢
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2021-07-24
      • 2019-06-15
      • 2015-06-30
      • 1970-01-01
      相关资源
      最近更新 更多