刚创建了一个带有ZF2的表单,它给出了错误

时间:2017-03-13 06:38:26

标签: forms input zend-framework2

当我访问新创建的表单时出现以下错误:

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}

如何解决上述错误并使模块正常工作。 如果需要进一步的代码来探索这个问题,请告诉我。

1 个答案:

答案 0 :(得分:1)

我们在评论中修正了问题,但是我发布了答案,所以如果有人有类似的问题,可能会有所帮助。

首先。检查您的Zend库是否是最新的 如果是这样,那么尝试检查问题是否只有一个特定输入(注释/删除临时)或它是全局问题。也许你只是在输入类型中输入错误(Zend可能会抛出给定类型不存在的异常)。

如果您被弃用信息:

  

您正在从班级[...]

中检索服务定位器

请记住,Zend在将来的版本中将删除getServiceLocator()方法,因此建议不要在控制器中使用服务定位器。您应该通过工厂类注入依赖项。