0

我对 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)
4

4 回答 4

0

请看你的 getPhonebookItem 函数,可能你什么都没有返回。您必须返回一些 $row。

像这样...

$row = $rowset->current();

if(!$row) {
    throw new \Exception("Article #$id not found");
}
return $row; //!!!!!!!!!!!!!
于 2013-10-23T15:52:30.937 回答
0

我的情况的错误是当我试图使用 Doctrine 的实体管理器检索一个实体时,它返回了 null。然后我将一个 null/false 值传递给 $form->bind():

    $form->bind( $requestEntity );

这产生了错误。

于 2014-06-18T16:52:48.973 回答
0

$phonebook您的变量似乎为空。因此getPhonebookItem(),如果没有找到记录,您的方法似乎不会抛出异常。

于 2013-09-02T13:36:03.153 回答
0
于 2015-01-22T09:06:04.393 回答