2016-06-21 60 views
0

我想預填充表單的下拉通過靜態存儲在Zend的2 module.config.php數據選項,並正在到這帶來一個問題形式:注入變量納入__construct功能Zend的2

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

    } 



} 

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

回答

0

我結束在module.config.php(去掉'ILL-form'行)的服務管理器中取出表單並從inde中調用它xControllerFactory.php代替

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

$controller->setILLForm($create_ill); 

,而不是

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