dongtu7567 2018-03-28 15:50
浏览 29
已采纳

Symfony FOS UserBundle:覆盖错误登陆页面

I'm overriding the forms on my Symfony app, but I surely skipped something in the process, but I do not know what.

Basically, it all works fine and looking as I want it to, but as soon as I purposely generate an error(i.e : changing email address to an invalid one), I am redirected to the lonely form template, instead of reloading to my page with the generated and displaying the problem.

I tried replacing this line :

return $this->render('@FOSUser/Profile/edit.html.twig', array(
'form' => $form->createView(),
 ));

from the ProfileController, as I think it is the reason, but I'm doing it wrong and getting errors when I try.

What would be the correct syntax to go my customized profile page that contains other forms, while displaying the errors of the submitted form ?

  • 写回答

1条回答 默认 最新

  • dpwo36915 2018-03-29 13:36
    关注

    I assume you have already overridden FOSUserBundle by your own UserBundle (as explained in the official documentation). Then, you have to modify the function editAction() in your own ProfileController, and write the twig template of your UserBundle profile page (look at my last comment in the code below):

    <?php
    // src/UserBundle/Controller/ProfileController.php
    
    namespace UserBundle\Controller;
    
    // use statements
    
    class ProfileController extends Controller
    {
        /**
         * Edit the user.
         *
         * @param Request $request
         *
         * @return Response
         */
        public function editAction(Request $request)
        {
            $user = $this->getUser();
            if (!is_object($user) || !$user instanceof UserInterface) {
                throw new AccessDeniedException('This user does not have access to this section.');
            }
    
            /** @var $dispatcher EventDispatcherInterface */
            $dispatcher = $this->get('event_dispatcher');
    
            $event = new GetResponseUserEvent($user, $request);
            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
    
            if (null !== $event->getResponse()) {
                return $event->getResponse();
            }
    
            /** @var $formFactory FactoryInterface */
            $formFactory = $this->get('fos_user.profile.form.factory');
    
            $form = $formFactory->createForm();
            $form->setData($user);
    
            $form->handleRequest($request);
    
            if ($form->isSubmitted() && $form->isValid()) {
                /** @var $userManager UserManagerInterface */
                $userManager = $this->get('fos_user.user_manager');
    
                $event = new FormEvent($form, $request);
                $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
    
                $userManager->updateUser($user);
    
                if (null === $response = $event->getResponse()) {
    
                    $url = $this->generateUrl('fos_user_profile_show');
                    $response = new RedirectResponse($url);
                }
    
                $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
    
                return $response;
            }
    
            // Change the following line, with your custom profile twig template
            //return $this->render('@FOSUser/Profile/edit.html.twig', array(
            return $this->render('UserBundle:Profile:edit.html.twig', array(
                'form' => $form->createView(),
            ));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上