将变量注入表单__construct函数Zend 2

时间:2016-06-21 05:17:18

标签: forms zend-framework2

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

  1. 我尝试在__construct()函数中获取Servicemanager但它不可用。
  2. 我将元素声明移动到表单类中的另一个函数,这样我就可以将变量传递给它,但是视图控制器找不到元素。
  3. 我目前通过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工厂:

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

1 个答案:

答案 0 :(得分:0)

我最终从module.config.php中的服务管理器invokables中删除了表单(删除了' ILL-form'的行),然后从indexControllerFactory.php中调用它

$create_ill = new Form\ILLForm('create_ill', $campuses, $illCategories, $getFromOptions);

$controller->setILLForm($create_ill);

而不是

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