加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 覆盖SonataUserBundle的ProfileFormType

发布时间:2020-12-13 22:44:21 所属栏目:PHP教程 来源:网络整理
导读:我试图覆盖SonataUserBundle的ProfileFormType. 我在该表单中添加了一些额外的字段,并在页面上呈现了所有字段.这样才行.但现在我想知道为什么用户的数据没有加载,因为firstname,lastname,…已经知道但只是没有显示在表单的文本框中. 重写的ProfileController
我试图覆盖SonataUserBundle的ProfileFormType.
我在该表单中添加了一些额外的字段,并在页面上呈现了所有字段.这样才行.但现在我想知道为什么用户的数据没有加载,因为firstname,lastname,…已经知道但只是没有显示在表单的文本框中.

重写的ProfileController类的editProfileAction:

/**
 * @return Response
 *
 * @throws AccessDeniedException
 */
public function editProfileAction()
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    $form = $this->container->get('sonata.user.profile.form');
    $formHandler = $this->container->get('sonata.user.profile.form.handler');

    $process = $formHandler->process($user);
    if ($process) {
        $this->setFlash('fos_user_success','profile.flash.updated');

        return new RedirectResponse($this->generateUrl('sonata_user_profile_show'));
    }

    // This doesn't show the firstname
    die($form->getData()->getFirstname());

    return $this->render('SonataUserBundle:Profile:edit_profile.html.twig',array(
        'form' => $form->createView(),'theme' => $this->container->getParameter('fos_user.template.theme')
    ));
}

重写的ProfileFormHandler类的进程函数:

public function process(UserInterface $user)
{
    $this->form->setData($user);

    // This DOES show the firstname 
    die($this->form->getData()->getFirstname());

    if ('POST' == $this->request->getMethod()) {
        $this->form->bindRequest($this->request);

        if ($this->form->isValid()) {
            $user->upload();
            $this->onSuccess($user);

            return true;
        }

        // Reloads the user to reset its username. This is needed when the
        // username or password have been changed to avoid issues with the
        // security layer.
        $this->userManager->reloadUser($user);
    }

    return false;
}

Services.yml:

services:
    application_sonata_user.registration.form.type:
        class: ApplicationSonataUserBundleFormTypeRegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type,alias: application_sonata_user_registration }

    application_sonata_user.profile.form.type:
        class: ApplicationSonataUserBundleFormTypeProfileType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type,alias: application_sonata_user_profile }

    application_sonata_user.search.form.type:
        class: ApplicationSonataUserBundleFormTypeSearchFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type,alias: application_sonata_user_search }

    application_sonata_user.form.handler.profile:
        class: ApplicationSonataUserBundleFormHandlerProfileFormHandler
        arguments: ["@fos_user.profile.form","@request","@fos_user.user_manager","@ewz_search.lucene"]
        scope: request
        public: false

解决方法

在services.yml文件中,我不得不放:

arguments: ["@sonata.user.profile.form","@ewz_search.lucene"]

代替

arguments: ["@fos_user.profile.form","@ewz_search.lucene"]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读