I'm learning ZF2 and trying to create a form but whenever i run the url that calls the form action i get the following message:
Zend\Form\Fieldset::add requires that $elementOrFieldset be an object implementing Zend\Form\ElementInterface; received "string"
My stack if the following:
#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}
I create the Form class like this:
<?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');
}
}
And I called the infocontroller with the form like so:
...
public function infoAction()
{
$form = new Info();
if ($this->request->isPost())
{
$form->setData($this->request->getPost());
// save stuff
}
return new ViewModel(array(
'form' => $form,
));
}
Am I missing something or did I needed to create this fieldset class and that's why it's giving me this error? Also if anyone has some good zf2 tutorials to send me that would be very nice.
答案 0 :(得分: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很好,你试过吗?