刚刚用 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}

我如何解决上述错误并使模块正常工作。让我知道是否需要进一步的代码来探索这个问题。

西蒙·M。

我们在评论中修复了问题,但我发布了答案,因此如果有人遇到类似问题,这可能会有所帮助。

首先。检查您的 Zend 库是否是最新的。
如果是这样,请尝试检查问题是否仅与某个特定输入有关(注释/临时删除),还是全局问题。也许您只是在输入类型上打错了字(Zend 可能会抛出给定类型不存在的异常)。

如果您不赞成使用此信息:

您正在从类中检索服务定位器 [...]

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章