我对 bind() 表单有疑问。当我在 editAction 中调用 bind 时出现此错误。错误是:
Zend\Form\Exception\InvalidArgumentException
文件:
/var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Form/Fieldset.php:439
信息:
Zend\Form\Fieldset::setObject expects an object argument; received ""
堆栈跟踪:
#0 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Form/Form.php(271): Zend\Form\Fieldset->setObject(NULL)
#1 /var/www/html/zf/zf-local/module/Phonebook/src/Phonebook/Controller/PhonebookController.php(123): Zend\Form\Form->bind(NULL)
#2 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Mvc/Controller/AbstractActionController.php(83): Phonebook\Controller\PhonebookController->editAction()
#3 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#4 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#5 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#8 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 /var/www/html/zf/zf-local/public/index.php(12): Zend\Mvc\Application->run()
#13 {main}
当我删除 bind() 所有成功执行但当我调用 form->bind 时出现此错误。我阅读了文档并再次阅读了一些。
我的编辑操作是:
/**
* Edit Action
*/
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('phonebook', array(
'action' => 'add'
));
}
// Get the Phonebook with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$phonebook = $this->getPhonebookTable()->getPhonebookItem($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('phonebook', array(
'action' => 'index'
));
}
$form = new PhonebookForm();
$form->bind($phonebook);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($phonebook->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getPhonebookTable()->saveItem($phonebook);
// Redirect to list of albums
return $this->redirect()->toRoute('phonebook');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
调试$phonebook:
object(Phonebook\Model\Phonebook)[254]
protected 'filter' => null
public 'id' => string '1' (length=1)
public 'firstname' => string 'Ivan' (length=4)
public 'lastname' => string 'Stojkovic' (length=11)
public 'homephone' => string '0616278054' (length=10)
public 'workphone' => string '013351456' (length=9)
public 'company' => string 'Delix' (length=5)
public 'email' => string 'office.stojmenovic@gmail.com' (length=28)
public 'address' => string 'Voislava Ilica 8' (length=16)
public 'city' => string 'Pancevo' (length=7)
public 'fax' => string '21565486' (length=8)