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

时间:2014-11-11 13:56:01

标签: 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
{
    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();
    }
}

我收到此错误后会出现此错误:

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.

据我所知,表格有问题。但是我真的不明白为什么,因为同样的观点,在我发送它之前看起来非常好。

1 个答案:

答案 0 :(得分:2)

问题在于addMethod

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

反过来在这里使用而不处理return可能性:

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

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

通过在if语句中返回$this->redirect,您可以提供两个潜在的返回值addMethodFormViewRedirectResponse。因此,您尝试将RedirectResponse传递给Twig尝试渲染的form(当然,它无法实现。)

解决方案是重新运行返回逻辑!