【问题标题】:FosUserBundle Profile Edit form submission "The CSRF token is invalid. " error. Even though the _token value is present in the formFosUserBundle Profile Edit 表单提交“CSRF 令牌无效。”错误。即使 _token 值存在于表单中
【发布时间】:2014-08-23 13:10:54
【问题描述】:

我已经覆盖了 fosuserbundle 的注册和配置文件编辑表单,但没有覆盖它们的表单处理程序。注册表按预期工作。但是提交配置文件编辑表单没有效果。当我追踪失败的地方时,我发现它没有通过 FOSUserBundle:ProfileFormHandler 中的$this->form->isValid()。当我 var_dump 错误时,它显示“CSRF 令牌无效......”但我的表单正在呈现 _token 值。

这是我的代码:

重写 Profile Controller 的 editAction:

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 YYT\UserBundle\Form\Type;

use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
use YYT\SharedBundle\Form\Type\AddressType;

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: YYT\UserBundle\Form\Type\RegistrationFormType
          arguments: [%fos_user.model.user.class%]
          tags:
              - { name: form.type, alias: yyt_user_registration }
  yyt_user.profile.form.type:
          class: YYT\UserBundle\Form\Type\ProfileFormType
          arguments: [%fos_user.model.user.class%]
          tags:
              - { name: form.type, alias: yyt_user_profile }

app/config/config.yml

...
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: YYT\UserBundle\Entity\User
    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}) }}

我错过了什么?

【问题讨论】:

  • 您找到解决方案了吗?

标签: symfony fosuserbundle


【解决方案1】:
  {{ form_end(form, { "render_rest":false}) }}  

您没有使用 csrf 令牌渲染字段。删除"render_rest":false或手动添加csrf令牌&lt;input type="hidden" name="_csrf_token" value="{{ csrf_token('intention') }}"&gt;

【讨论】:

  • 我已经尝试了您推荐的两种方法,但没有任何变化。但我注意到 {{ form_end(form) }} 呈现此字段: 这与名称不同您在上面发布的隐藏输入。如果此信息有帮助。
猜你喜欢
  • 2017-08-01
  • 2014-04-15
  • 2014-06-20
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 2020-07-27
  • 2018-01-15
相关资源
最近更新 更多