Php 在同一页面上发布/重定向/获取后出现Symfony表单呈现异常

Php 在同一页面上发布/重定向/获取后出现Symfony表单呈现异常,php,forms,symfony,Php,Forms,Symfony,这段代码工作得很好: use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; abstract class TableManagerController extends Controller { publi

这段代码工作得很好:

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()
        ));
    }
}
但是当我把它分成3种方法时,就像这样:

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();
    }
}
我收到此错误,该错误在我发送表单后出现:

在呈现模板期间引发了异常(“可捕获的致命错误:参数1传递给Symfony\Component\Form\FormRenderer::renderBlock())必须是Symfony\Component\Form\FormView的实例,Symfony\Component\HttpFoundation\RedirectResponse的实例,在第61行的D:\Websites\CPG-2015\app\cache\dev\twig\d6\80\0e5eee6c7aa1859cedb4cd0cc7317a0ebbd61af7e80f217ce1d2cf86771.php中调用,并在D:\Websites\CPG-2015\vendor\Symfony\Symfony\src\Symfony\Form\src\Symfony\Component\Form中定义IBCPGAdministrationBundle:CourseLevel:index.html.twig第19行的enderer.php第106行“。


据我所知,表格有问题。但是我真的不明白为什么,因为在我发送它之前,这个相同的表单,从相同的视图,看起来非常好。

问题在您的
添加方法中:

        // We redirect user to the defined homepage
        return $this->redirect($this->generateUrl($this->routeHomePage));
然后在这里使用,而不处理该
返回
可能性:

$form = $this->addMethod($request);

    return $this->render($this->renderIndexTemplate, array(
        'dataList' => $dataList,
        'form' => $form
    ));
通过在if语句中返回
$this->redirect
,您将给出两个潜在的返回值
addMethod
,一个
FormView
或一个
RedirectResponse
。因此,您可以尝试将
重定向响应
传递给
表单
哪个细枝尝试渲染(当然,它不能)

解决方案是重新设计您的返回逻辑