将变量注入 Forms __construct 函数 Zend 2

Injecting variables into Forms __construct function Zend 2

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

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

我目前通过 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);

     }



}

调用窗体的Indexcontroller Factory:

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',
    ),

我最终从 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'));