【问题标题】:Symfony FOS UserBundle: override error landing pageSymfony FOS UserBundle:覆盖错误登陆页面
【发布时间】:2018-03-28 15:50:34
【问题描述】:

我正在覆盖我的 Symfony 应用程序上的表单,但我肯定在此过程中跳过了某些内容,但我不知道是什么。

基本上,一切正常并且看起来如我所愿,但是一旦我故意生成错误(即:将电子邮件地址更改为无效的),我就会被重定向到孤独的表单模板,而不是重新加载到我的页面,生成并显示问题。

我尝试替换这一行:

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

来自 ProfileController,我认为这是原因,但我做错了,尝试时出错。

在显示提交表单的错误时,转到包含其他表单的自定义个人资料页面的正确语法是什么?

【问题讨论】:

    标签: php symfony overriding fosuserbundle


    【解决方案1】:

    我假设您已经用自己的 UserBundle 覆盖了 FOSUserBundle(如 official documentation 中所述)。然后,你必须修改你自己的ProfileController中的函数editAction(),并编写你的UserBundle个人资料页面的twig模板(看看我在下面代码中的最后一条评论):

    <?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(),
            ));
        }
    }
    

    【讨论】:

    • 重写控制器是我忘记的一件事 叹息……哈哈,现在一些 ajax,我会得到我想要的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-06-28
    • 2013-01-25
    • 2014-07-24
    • 1970-01-01
    • 2014-10-16
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多