FosUserBundle 配置文件 编辑表单提交"The CSRF token is invalid. "错误。即使_token值存在于表单中



我已经覆盖了fosuserbundle的注册和配置文件编辑表单,但没有覆盖它们的表单处理程序。注册表按预期工作。但是提交个人资料编辑表单不起作用。当我跟踪它失败的地方时,我发现它没有传递FOSUserBundle:ProfileFormHandler中的$this->form->isValid()。当我var_dump错误时,它说"CSRF 令牌无效......"但我的表单正在呈现_token值。

这是我的代码:

覆盖配置文件控制器的编辑操作:

public function editAction()
    {
        $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('fos_user.profile.form');
        $form = $this->container->get('form.factory')->create('yyt_user_profile', $user);
        $formHandler = $this->container->get('fos_user.profile.form.handler');
        $process = $formHandler->process($user);
        if ($process) {
            $this->setFlash('fos_user_success', 'profile.flash.updated');
            return new RedirectResponse($this->getRedirectionUrl($user));
        }
        return $this->container->get('templating')->renderResponse(
            'UserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
            array('form' => $form->createView(), 'active_page' => 'Profile')
        );
    }

我的个人资料表格类型:

namespace YYTUserBundleFormType;
use SymfonyComponentFormFormBuilderInterface;
use FOSUserBundleFormTypeProfileFormType as BaseType;
use YYTSharedBundleFormTypeAddressType;
class ProfileFormType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder->add('fullName')
            ->add('address', new AddressType(), array('label' => false))
        ;
    }
    public function getName()
    {
        return 'yyt_user_profile';
    }
}

我的服务.yml

services:
  yyt_user.registration.form.type:
          class: YYTUserBundleFormTypeRegistrationFormType
          arguments: [%fos_user.model.user.class%]
          tags:
              - { name: form.type, alias: yyt_user_registration }
  yyt_user.profile.form.type:
          class: YYTUserBundleFormTypeProfileFormType
          arguments: [%fos_user.model.user.class%]
          tags:
              - { name: form.type, alias: yyt_user_profile }

The app/config/config.yml

...
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: YYTUserBundleEntityUser
    registration:
        form:
            type: yyt_user_registration
    profile:
        form:
            type: yyt_user_profile

我的个人资料表单类型:

还有我最重要的edit_content.html.twig:

<form action="{{ path('fos_user_profile_edit') }}" {{ form_enctype(form) }} method="POST" class="fos_user_profile_edit">
    <div class="col-md-7">
        <div class="form-group row">
            {{ form_row(form.fullName) }}
        </div>
        <div class="form-group row">
            {{ form_row(form.username) }}
        </div>
        <div class="form-group row">
            {{ form_row(form.email) }}
        </div>
        <div class="form-group row">
            {{ form_row(form.address.addrTelephone) }}
        </div>
        <div class="form-group row">
            {{ form_row(form.address.addrMobile) }}
        </div>
        <div class="form-group row">
            {{ form_row(form.address.addrSubCity) }}
        </div>
        <div class="form-group row">
            {{ form_row(form.current_password) }}
        </div>
    </div>
    <div class="col-md-12">
        <input class="btn btn-primary" type="submit" value="{{ 'profile.edit.submit'|trans({}, 'FOSUserBundle') }}" />
    </div>
    {{ form_end(form, { "render_rest":false}) }}

我在这里错过了什么?

  {{ form_end(form, { "render_rest":false}) }}  

您没有使用 csrf 令牌呈现字段。删除"render_rest":false或手动添加 csrf 令牌<input type="hidden" name="_csrf_token" value="{{ csrf_token('intention') }}">

最新更新