Symfony2,保持表单数据跨重定向

时间:2015-12-28 19:19:38

标签: forms validation symfony session-variables url-redirection

如何在重定向期间使用会话检索数据?我收到错误消息:“异常'Symfony \ Component \ Form \ Exception \ AlreadySubmittedException',消息'您无法更改提交表单的数据。”

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \控制器\ UserController.php

/**
 * Creates a new User entity.
 *
 * @Route("/new", name="user_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request)
{
    $user = new User();
    $form = $this->createForm(new UserType(), $user);
    $form->handleRequest($request);
    $session = $this->getRequest()->getSession();

    $form->setData(unserialize($session->get('userFilter')));        

    if ( $form->isSubmitted() && $form->isValid() ) {
        $session->set( 'userFilter', serialize($form->getData()) ); 

            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            return $this->redirectToRoute('user_edit', array('id' => $user->getId()));

    } 
    return $this->render('MeetingBundle::user/new.html.twig', array(
        'user' => $user,
        'form' => $form->createView(),
    ));

} //   public function newAction(Request $request)   

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj3_27 \ SRC \ MeetingBundle \事件监听\ ExceptionListener.php

<?php
namespace MeetingBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

//every time the Kernel throws the kernel.exception event, the function onKernelException() is called.
/* also must create service :
    meeting.exception_listener:
        class: MeetingBundle\EventListener\ExceptionListener
        arguments: [@templating, @kernel, @router]
        tags:
            - { name: kernel.event_listener, event: kernel.exception, method: onKernelException  }    
 */

class ExceptionListener  
{
    protected $templating;
    protected $kernel;
    protected $router; 

    public function __construct( EngineInterface $templating, $kernel, Router $router)
    {
        $this->templating = $templating;
        $this->kernel = $kernel;
        $this->router = $router;
    }    

     public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        $request=$event->getRequest();
        $referer = $event->getRequest()->headers->get('referer');       

        $msg="";


        $excStr=$exception->__toString(); // returns string finally! 
        $bdup=strpos( $excStr , 'Integrity constraint violation: 1062 Duplicate entry' );

        if($bdup) { 
            $msg=" This username is already taken. Choose another username. ";           
        }

        if(strlen($msg)!=0) {
            // flash messsages are displayed in layout.html
            $request->getSession()
            ->getFlashBag()
            ->add('Error', $msg);
        }

        $response = new RedirectResponse($referer); // redirect to the error page

        if ($exception instanceof HttpExceptionInterface) {
            $response->setStatusCode($exception->getStatusCode());
            $response->headers->replace($exception->getHeaders());
        } else {
            $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        $event->setResponse($response);

    }   

}

0 个答案:

没有答案