【问题标题】:Zend Framework 2 Form Fieldset ErrorZend Framework 2 表单字段集错误
【发布时间】:2017-01-08 09:24:54
【问题描述】:

我正在学习 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【内部函数】: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), 对象(闭包))

#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\请求),对象(Zend\Http\PhpEnvironment\Response))

#9【内部函数】: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), 对象(闭包))

#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 {主}

我这样创建 Form 类:

<?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');
    }
}

我用这样的形式调用了信息控制器:

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

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

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

【问题讨论】:

    标签: php forms zend-framework zend-framework2 fieldset


    【解决方案1】:

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

      $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教程的问题,offical one很好,你试过了吗?

    【讨论】:

    • 猜我看错了教程。我也在关注那个教程,但它让我有点困惑。也许 ZF 目前对我来说太先进了,但我正在努力。这样就成功了,并且能够使表单正常工作。谢谢