【问题标题】:Call to undefined method can't create createDeleteForm调用未定义的方法无法创建 createDeleteForm
【发布时间】:2013-08-02 13:51:52
【问题描述】:

我需要在我的视图中创建一个 deleteForm,所以我这样做:

/**
 * Show created bank account
 *
 * @Route("/{account_id}", name="wba_show")
 * @Method("GET")
 */
public function showAction($account_id) {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('BankBundle:Account')->find($account_id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Account entity.');
        }

        $deleteForm = $this->createDeleteForm($account_id);
        return array('entity' => $entity, 'delete_form' => $deleteForm->createView());
}

但我收到此错误:

FatalErrorException:错误:调用未定义的方法 BankBundle\Controller\WController::createDeleteForm() 在 /var/www/html/src/BankBundle/Controller/WController.php 第 66 行

这个方法有什么问题?我需要使用一些东西来创建表单吗?

解决方案 我找到了解决方案,因为 createDeleteForm() 不是 Symfony2 方法,是我从另一个代码中获取的错误,所以我以这种方式创建函数:

private function createDeleteForm($account_id) {
        return $this->createFormBuilder(array('account_id' => $id))->add('account_id', 'hidden')->getForm();
}

瞧,问题消失了!!

【问题讨论】:

    标签: symfony symfony-forms symfony-2.3


    【解决方案1】:

    你需要创建一个这样的表单:

    $this->createForm(new CreateDeleteType());
    

    当然你还需要创建这个表单:

    class CreateDeleteType extends AbstractType {
    

    除此之外,您的错误信息非常清楚。您呼叫的method (createDeleteForm) 不存在。请注意,$this 指的是您当前的控制器上下文,而不是表单上下文。阅读有关forms in the official docs 的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 2015-06-27
      相关资源
      最近更新 更多