2017-03-13 52 views
0

我有以下錯誤,當我有機會我的新創建的窗體:剛剛創建了一個形式ZF2及其給錯誤

Zend\Form\Factory::create expects the $spec["type"] to implement one of Zend\Form\ElementInterface, Zend\Form\FieldsetInterface, or Zend\Form\FormInterface; received Hidden 

這裏是我的AlbumForm.php文件:

namespace Album\Form; 

use Zend\Form\Form; 

class AlbumForm extends Form 
    { 
public function __construct($name = null) 
{ 
    // we want to ignore the name passed 
    parent::__construct('album'); 

    $this->add(array(
     'name' => 'id', 
     'type' => 'Hidden', 
    )); 
    $this->add(array(
     'name' => 'title', 
     'type' => 'Text', 
     'options' => array(
      'label' => 'Title', 
     ), 
    )); 
    $this->add(array(
     'name' => 'artist', 
     'type' => 'Text', 
     'options' => array(
      'label' => 'Artist', 
     ), 
    )); 
    $this->add(array(
     'name' => 'submit', 
     'type' => 'Submit', 
     'attributes' => array(
      'value' => 'Go', 
      'id' => 'submitbutton', 
     ), 
    )); 
} 
} 

這裏是表單模型Album.php代碼:

// Add content to these methods: 
public function setInputFilter(InputFilterInterface $inputFilter) 
{ 
    throw new \Exception("Not used"); 
} 

public function getInputFilter() 
{ 
    if (!$this->inputFilter) { 
     $inputFilter = new InputFilter(); 

     $inputFilter->add(array(
      'name'  => 'id', 
      'required' => true, 
      'filters' => array(
       array('name' => 'Int'), 
      ), 
     )); 

     $inputFilter->add(array(
      'name'  => 'artist', 
      'required' => true, 
      'filters' => array(
       array('name' => 'StripTags'), 
       array('name' => 'StringTrim'), 
      ), 
      'validators' => array(
       array(
        'name' => 'StringLength', 
        'options' => array(
         'encoding' => 'UTF-8', 
         'min'  => 1, 
         'max'  => 100, 
        ), 
       ), 
      ), 
     )); 

     $inputFilter->add(array(
      'name'  => 'title', 
      'required' => true, 
      'filters' => array(
       array('name' => 'StripTags'), 
       array('name' => 'StringTrim'), 
      ), 
      'validators' => array(
       array(
        'name' => 'StringLength', 
        'options' => array(
         'encoding' => 'UTF-8', 
         'min'  => 1, 
         'max'  => 100, 
        ), 
       ), 
      ), 
     )); 

     $this->inputFilter = $inputFilter; 
    } 

    return $this->inputFilter; 
} 

這裏是堆棧跟蹤:

#0 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Form\Form.php(143): Zend\Form\Factory->create(Array) 
#1 C:\xampp\htdocs\zendtest\module\Album\src\Album\Form\AlbumForm.php(22): Zend\Form\Form->add(Array) 
#2 C:\xampp\htdocs\zendtest\module\Album\src\Album\Controller\AlbumController.php(41): Album\Form\AlbumForm->__construct() 
#3 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Mvc\Controller\AbstractActionController.php(87): Album\Controller\AlbumController->addAction() 
#4 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent)) 
#5 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) 
#6 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\EventManager\EventManager.php(208): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 
#7 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Mvc\Controller\AbstractController.php(108): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 
#8 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Mvc\DispatchListener.php(113): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response)) 
#9 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent)) 
#10 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) 
#11 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\EventManager\EventManager.php(208): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 
#12 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Mvc\Application.php(297): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 
#13 C:\xampp\htdocs\zendtest\public\index.php(14): Zend\Mvc\Application->run() 
#14 {main} 

我如何解決上述錯誤,並使模塊正常工作。 讓我知道是否需要進一步的代碼來探索這個問題。

+0

你確定這個錯誤來自AlbumForm嗎?檢查你的堆棧跟蹤。你有沒有檢查表單是否工作沒有隱藏的輸入? – SzymonM

+0

是的,我通過更改刪除隱藏的字段,但仍然是相同的錯誤檢查。請檢查堆棧跟蹤錯誤的更新問題: – Shoaib

+0

您使用的是什麼版本的Zend?它看起來像舊版本。如果您已經通過作曲者安裝了它,請執行'$ composer update' – SzymonM

回答

1

我們在評論中修復了問題,但我發佈了答案,所以如果有人遇到類似問題,可能會有所幫助。

首先。檢查你的Zend庫是否是最新的。
如果是這樣,那麼嘗試檢查問題是否只有一個輸入(評論/刪除臨時)或它是全球性的問題。也許你只是在輸入類型中輸入錯誤(Zend可能會拋出給定類型不存在的異常)。

如果你過時的信息:

You are retrieving the service locator from within the class [...]

請記住,Zend公司在未來的版本將刪除getServiceLocator()方法,因此使用服務定位器控制器不推薦。你應該通過工廠類注入你的依賴關係。

相關問題