Php Zend Framework 2表单字段集错误
我正在学习ZF2并尝试创建表单,但每当我运行调用表单操作的url时,我都会收到以下消息:Php Zend Framework 2表单字段集错误,php,forms,zend-framework,zend-framework2,fieldset,Php,Forms,Zend Framework,Zend Framework2,Fieldset,我正在学习ZF2并尝试创建表单,但每当我运行调用表单操作的url时,我都会收到以下消息: Zend\Form\Fieldset::add requires that $elementOrFieldset be an object implementing Zend\Form\ElementInterface; received "string" 如果出现以下情况,则显示我的堆栈: #0/Users/cesar/Documents/zf2course/vendor/zendf
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->\uu 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(对象(Zend\Mvc\MvcEvent))
#5/Users/cesar/Documents/zf2course/vendor/zendframework/zend eventmanager/src/eventmanager.php(490):调用_user_func(数组,对象(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(对象(zend\Http\PhpEnvironment\Request)、对象(zend\Http\PhpEnvironment\Response))
#9[内部函数]:Zend\Mvc\DispatchListener->onDispatch(对象(Zend\Mvc\MvcEvent))
#10/Users/cesar/Documents/zf2course/vendor/zendframework/zend eventmanager/src/eventmanager.php(490):调用用户函数(数组,对象(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(对象(闭包),对象(zend\mvc\MvcEvent))
#13/Users/Cesar/Documents/zf2course/public/index.php(21):Zend\Mvc\Application->run()
#14{main}
我创建的表单类如下所示:
<?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');
}
}
我是否遗漏了什么,或者我是否需要创建这个字段集类,这就是为什么它会给我这个错误?
另外,如果有人给我发了一些好的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教程的问题,这是一个很好的问题,您试过了吗?我想我看错了教程。我也在学习那个教程,但它让我有点困惑。也许ZF目前对我来说太先进了,但我正在努力。这样做的技巧,并能够使形式的工作。谢谢
$this->add($location);
$this->add($sizeW);
$this->add($sizeH);
$this->add($type);
$this->add($family);
$this->add($diff);
$this->add($submit);