Zend Framework 2 表单字段集错误

Zend Framework 2 Form Fieldset Error

我正在学习 ZF2 并尝试创建一个表单,但是每当我 运行 调用表单操作的 url 时,我都会收到以下消息:

Zend\Form\Fieldset::add requires that $elementOrFieldset be an object implementing Zend\Form\ElementInterface; received "string"

我的堆栈如下:

#0 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-form/src/Form.php(179): Zend\Form\Fieldset->add('location', Array)

#1 /Users/cesar/Documents/zf2course/module/Application/src/Application/Form/Info.php(69): Zend\Form\Form->add('location')

#2 /Users/cesar/Documents/zf2course/module/Application/src/Application/Controller/IndexController.php(25): Application\Form\Info->__construct()

#3 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(82): Application\Controller\IndexController->infoAction()

#4 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))

#5 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-eventmanager/src/EventManager.php(490): call_user_func(Array, Object(Zend\Mvc\MvcEvent))

#6 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-eventmanager/src/EventManager.php(263): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))

#7 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(118): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))

#8 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-mvc/src/DispatchListener.php(118): 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 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-eventmanager/src/EventManager.php(490): call_user_func(Array, Object(Zend\Mvc\MvcEvent))

#11 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-eventmanager/src/EventManager.php(263): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))

#12 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-mvc/src/Application.php(340): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))

#13 /Users/Cesar/Documents/zf2course/public/index.php(21): Zend\Mvc\Application->run()

#14 {main}

我这样创建表单 class:

<?php
namespace Application\Form;

use Zend\Form\Form;
use Zend\Form\Element;

class Info extends Form
{
    public function __construct() 
    {
      parent::__construct('info'); 
      
      $location = new Element('location');
      $location->setLabel('Location');
      $location->setAttribute(array(
          'type' => 'text',
          'class' => 'form-control',
      ));
      
      $sizeW = new Element\Number('size_w');
      $sizeW->setLabel('Width Size');
      $sizeW->setAttributes(array(
                'min'  => '0',
                'max'  => '500',
                'step' => '0.1', 
                'class' => 'form-control'
            ));

      $sizeH = new Element\Number('size_h');
      $sizeH->setLabel('Height Size');
      $sizeH->setAttributes(array(
                'min'  => '0',
                'max'  => '500',
                'step' => '0.1', 
                'class' => 'form-control'
            ));    
      
      $type = new Element\Select('plot_type');
      $type->setLabel('Plot Type');
      $type->setAttribute('class', 'form-control');
      $type->setValueOptions(array(
          1 => 'Balcony',
          2 => 'Plot',
      ));

      $family = new Element\Number('family');
      $family->setLabel('Family Aggregate Number');
      $family->setAttributes(array(
                'min'  => '0',
                'max'  => '10',
                'step' => '1', 
                'class' => 'form-control'
      ));
      
      $diff = new Element\Select('diff');
      $diff->setLabel('Gardening Type');
      $diff->setAttribute('class', 'form-control');
      $diff->setValueOptions(array(
          1 => 'Begginner',
          2 => 'Advanced',
      ));
      
      $submit = new Element\Submit('submit');
      $submit->setValue('Submit');
      $submit->setAttribute('class', 'btn btn-primary');
      
      $this->add('location');
      $this->add('size_w');
      $this->add('size_h');
      $this->add('plot_type');
      $this->add('family');
      $this->add('diff');
      $this->add('submit');
    }
}

我用这样的形式调用了 infocontroller:

...
   public function infoAction()
    {
        $form = new Info();
        
        if ($this->request->isPost()) 
        { 
            $form->setData($this->request->getPost());
            // save stuff

        }
        
        return new ViewModel(array(
            'form' => $form,
        ));
    }

我是不是遗漏了什么或者我是否需要创建这个字段集class,这就是它给我这个错误的原因? 另外,如果有人有一些好的 zf2 教程发给我,那就太好了。

为什么这些行带有字符串参数:

  $this->add('location');
  $this->add('size_w');
  $this->add('size_h');
  $this->add('plot_type');
  $this->add('family');
  $this->add('diff');
  $this->add('submit');

因为你只是在变量中定义了之前的所有元素?尝试用这些变量替换它们:

  $this->add($location);
  $this->add($sizeW);
  $this->add($sizeH);
  $this->add($type);
  $this->add($family);
  $this->add($diff);
  $this->add($submit);

关于ZF2 Tutorial的问题,offical one很好,你试过了吗?