Symfony Form,缺少CSRF令牌

时间:2013-11-19 11:21:06

标签: symfony field token csrf

就像在主题中一样,我遇到了CSRF令牌丢失的问题。这是我的表格:

$builder
            ->add('email', 'email', array(
                'label' => 'Adres e-mail'
            ))
            ->add('userFirstname', 'text', array(
                'label' => 'Imię',
                'required' => false
            ))
            ->add('userLastname', 'text', array(
                'label' => 'Nazwisko',
                'required' => false
            ))
            ->add('userBusiness', 'entity', array(
                'label' => 'Firma',
                'required' => false,
                'class' => 'Cloud\CrmBundle\Entity\RelationContact',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('u')->where("u.type = 'b'");
                },
                'empty_value' => true
            ))
            ->add('old_password', 'password', array(
                'label' => 'Stare hasło',
                'mapped' => false,
                'required' => false
            ))
            ->add('new_password', 'repeated', array(                
                'first_options' => array(
                    'label' => 'Nowe hasło'),
                'second_options' => array(
                    'label' => 'Powtórz nowe hasło'),
                'mapped' => false,
                'required' => false,
                'type' => 'password'
            ));

我的观点:

<div class="form-horizontal">
            {{ form_row(form.email) }}
            {{ form_row(form.userFirstname) }}
            {{ form_row(form.userLastname) }}
            {{ form_row(form.userBusiness) }}
        {{ form_row(form.old_password) }}
        {{ form_row(form.new_password) }}
        </div>
</div>

有什么不对的家伙?有任何想法吗? :(我只是不明白这个奇怪的错误......可能导致什么?

3 个答案:

答案 0 :(得分:2)

您可能需要手动添加此_token,因为您尝试手动显示表单:

{{ form_widget(form._token) }}

答案 1 :(得分:0)

Symfony2使用所需信息设置隐藏字段。为此,您必须包含隐藏字段:

{{ form_widget(form._token) }}

如果您不想要CSRF-Protection,则可以禁用参数文件中的dunction。

Disable symfony 2 csrf token protection on ajax submit

答案 2 :(得分:0)

如果您使用form_startform_end,symfony会自动将令牌字段添加到表单

<div class="form-horizontal">
    {{ form_start(form) }}
        {{ form_row(form.email) }}
        {{ form_row(form.userFirstname) }}
        {{ form_row(form.userLastname) }}
        {{ form_row(form.userBusiness) }}
        {{ form_row(form.old_password) }}
        {{ form_row(form.new_password) }}
    {{ form_end(form) }}
</div>
相关问题