dongqiao3214 2014-11-11 13:56
浏览 57
已采纳

Post / Redirect / Get在同一页面后的Symfony表单呈现异常

This code works just fine :

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

abstract class TableManagerController extends Controller
{
    public function listAndAddAction(Request $request)
    {
        // We get the Entity Manager
        $entityManager = $this->getDoctrine()->getManager();

        // We get the entity repository
        $repository = $entityManager->getRepository($this->entityRepository);

        // We build the new form through Form Factory service
        $form = $this->get('form.factory')->create($this->entityFormObject, $this->entityObject);

        // If user sent the form and sent data is valid
        if ($form->handleRequest($request)->isValid())
        {
            // We set the position of the new entity to the higher existing one + 1
            $newPosition = $repository->higherPosition() + 1;
            $this->entityObject->setPosition($newPosition);

            // We insert the data in DB
            $entityManager->persist($this->entityObject);
            $entityManager->flush();

            // We redirect user to the defined homepage
            return $this->redirect($this->generateUrl($this->routeHomePage));
        }

        return $this->render($this->renderIndexTemplate, array(
            'dataList' => $repository->listAll(),
            'form' => $form->createView()
        ));
    }
}

But when I just split it in 3 methods, like this :

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

abstract class TableManagerController extends Controller
{
    public function listAndAddAction(Request $request)
    {
        $dataList = $this->listMethod();
        $form = $this->addMethod($request);

        return $this->render($this->renderIndexTemplate, array(
            'dataList' => $dataList,
            'form' => $form
        ));
    }

    protected function listMethod()
    {
        // We get the Entity Manager
        $entityManager = $this->getDoctrine()->getManager();

        // We get the entity repository
        $repository = $entityManager->getRepository($this->entityRepository);

        // We generate the entity management homepage view (list + add form)
        return $repository->listAll();
    }

    protected function addMethod(Request $request)
    {
        // We get the Entity Manager
        $entityManager = $this->getDoctrine()->getManager();

        // We get the entity repository
        $repository = $entityManager->getRepository($this->entityRepository);

        // We build the new form through Form Factory service
        $form = $this->get('form.factory')->create($this->entityFormObject, $this->entityObject);

        // If user sent the form and sent data is valid
        if ($form->handleRequest($request)->isValid())
        {
            // We set the position of the new entity to the higher existing one + 1
            $newPosition = $repository->higherPosition() + 1;
            $this->entityObject->setPosition($newPosition);

            // We insert the data in DB
            $entityManager->persist($this->entityObject);
            $entityManager->flush();

            // We redirect user to the defined homepage
            return $this->redirect($this->generateUrl($this->routeHomePage));
        }

        // We return the generated form
        return $form->createView();
    }
}

I get this error which appears once I've sent the form :

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, instance of Symfony\Component\HttpFoundation\RedirectResponse given, called in D:\Websites\CPG-2015\app\cache\dev\twig\d6\80\0e5eee6c7aa1859cedb4cd0cc7317a0ebbdd61af7e80f217ce1d2cf86771.php on line 61 and defined in D:\Websites\CPG-2015\vendor\symfony\symfony\src\Symfony\Component\Form\FormRenderer.php line 106") in IBCPGAdministrationBundle:CourseLevel:index.html.twig at line 19.

for which I understand there is something wrong with the form. But I really don't get why since this same form, from the same view, appears perfectly well before I send it.

  • 写回答

1条回答 默认 最新

  • dongtang6718 2014-11-11 14:04
    关注

    The problem is here in your addMethod:

            // We redirect user to the defined homepage
            return $this->redirect($this->generateUrl($this->routeHomePage));
    

    which in turn gets used here without any handling of that return possibility:

    $form = $this->addMethod($request);
    
        return $this->render($this->renderIndexTemplate, array(
            'dataList' => $dataList,
            'form' => $form
        ));
    

    By returning $this->redirect inside of an if-statement, you're giving two potential return values of addMethod, a FormView or a RedirectResponse. As a result, you then try to pass that RedirectResponse through form which Twig attempts to render (which it can't, of course.)

    The solution is to re-work your return logic!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条