【问题标题】:Injecting variables into Forms __construct function Zend 2将变量注入 Forms __construct 函数 Zend 2
【发布时间】:2016-06-21 05:17:18
【问题描述】:

我正在尝试通过静态存储在 Zend 2 的 module.config.php 中的数据来预填充表单的下拉选项,但遇到了一个问题:

  1. 我尝试在 __construct() 函数中获取 Servicemanager,但它不可用。
  2. 我将元素声明移至表单类中的另一个函数,以便将变量传递给它,但视图控制器找不到元素。

我目前通过 Servicemanager Invokable 调用表单。如何将这些数组传递到表单的 __construct() 函数中?

代码如下:

class ILLForm extends Form
{
     public function __construct($fieldsetName, $campuses, $ILLTypes, $getFromOptions)
     {
        parent::__construct('create_ill');

        $this
             ->setAttribute('method', 'post')
             ->setHydrator(new ClassMethodsHydrator(false))
             ->setInputFilter(new InputFilter())
         ;


        $ill = new ILLFieldset('ill', $campuses, $ILLTypes, $getFromOptions);
        $ill->setName('ill')
            ->setOptions(array( 
               'use_as_base_fieldset' => true,
            ));

        $captcha = new Element\Captcha('captcha');
        $captchaAdapter = new Captcha\Dumb();
        $captchaAdapter->setWordlen(5);
        $captcha->setCaptcha($captchaAdapter)
                ->setLabelAttributes(array('class' => 'sq-form-question-title'))
                ->setAttribute('class', 'sq-form-field required')
                ->setLabel("* Captcha")
                ->setAttribute('title', 'Help to prevent SPAM');


        $submit = new Element\Submit('submit');
        $submit->setAttribute('value', 'Submit ILL')
                   ->setAttribute('class', 'sq-form-submit')
                   ->setAttribute('title', 'Submit ILL');

        $this->add($ill)
                ->add($captcha)
                ->add($submit);

     }



}

调用表单的索引控制器工厂:

class IndexControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $controllers)
    {
        $allServices = $controllers->getServiceLocator();
        $sm = $allServices->get('ServiceManager');
        $smConfig = $allServices->get('config');

        $campuses = $smConfig['campuses'];
        $illCategories = $smConfig['ILLCategories'];
        $getFromOptions = $smConfig['getFromOptions'];


        $controller = new IndexController();      
        $controller->setCampuses($campuses);
        $controller->setILLCategories($illCategories);
        $controller->setGetFromOptions($getFromOptions);
        $controller->setILLForm($sm->get('ILL-form'));
        $controller->setILLFormFilter($sm->get('ILL-form-filter'));
        //$controller->setParams($sm->get('params'));

        return $controller;
    }
}

以及相关的module.config.php摘录:

'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'invokables' => array(
        'ILL-form-filter'       => 'ILL\Form\ILLFormFilter',
        'ILL-form'              => 'ILL\Form\ILLForm',
    ),

【问题讨论】:

    标签: forms zend-framework2


    【解决方案1】:

    我最终从 module.config.php 中的服务管理器可调用项中取出表单(删除了“ILL-form”的行)并从 indexControllerFactory.php 中调用它

    $create_ill = new Form\ILLForm('create_ill', $campuses, $illCategories, $getFromOptions);
    
    $controller->setILLForm($create_ill);
    

    而不是

    $controller->setILLForm($sm->get('ILL-form'));
    

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 1970-01-01
      • 2018-12-04
      • 2014-11-05
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2015-06-24
      相关资源
      最近更新 更多