简体   繁体   English

Symfony2 - 清空CSRF令牌

[英]Symfony2 - Empty CSRF token

I deployed a Symfony2 app, but all the forms have empty csrf tokens. 我部署了一个Symfony2应用程序,但所有表单都有空的csrf标记。 This html comes from firebug. 这个html来自firebug。 As you can see, the tag is properly rendered, but no value is assigned. 如您所见,标记已正确呈现,但未分配任何值。

<input type="hidden" value="" name="category[_token]" id="category__token">

In my controller: 在我的控制器中:

$form = $this->createForm(new CategoryForm(), new Category());
$form->handleRequest($request);

if ($form->isValid()) {
    (...)
}

In the template, the form is inside of a bootstrap 3 modal. 在模板中,表单位于bootstrap 3模式内。

{{ form_start(form) }}
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">Voeg een tariefplan toe</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        {{ form_label(form.name) }}
                        {{ form_widget(form.name) }}
                    </div>
                    <div class="form-group">
                        {{ form_label(form.parent) }}
                        {{ form_widget(form.parent, {'attr':{ 'class': 'form-control' }}) }}
                    </div>
                    <div class="form-group">
                        {{ form_label(form.sizes) }}
                        {{ form_widget(form.sizes, {'attr':{ 'class': 'form-control' }}) }}
                    </div>
                    <div class="form-group">
                        {{ form_label(form.description) }}
                        {{ form_widget(form.description, {'attr':{ 'class': 'form-control' }}) }}
                    </div>

                    {{ form_widget(form._token) }}
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Annuleren</button>
                    {{ form_widget(form.save, {'attr':{ 'class': 'btn btn-primary' }}) }}
                </div>
            </div>
        </div>
{{ form_end(form) }}

I also created a gist for the CategoryForm class : http://goo.gl/6NWTkB . 我还为CategoryForm类创建了一个要点: http//goo.gl/6NWTkB

Anyone who knows what I'm missing here? 谁知道我在这里失踪了什么?

您必须提供以下值:

<input type="hidden" value={{ csrf_token('category') }}"" name="category[_token]" id="category__token">

A couple of ideas: 一些想法:

{{ form_end(form) }} should output your token so there's no need to have: {{ form_widget(form._token) }} . {{ form_end(form) }}应输出您的令牌,这样就不需要: {{ form_widget(form._token) }}

If you have a very large form php might truncate your request vars. 如果你有一个非常大的表单,php可能会截断你的请求变量。 Have a look at max_input_vars in your php.ini . 看看你的php.ini中的max_input_vars Default is 1000 I believe. 我相信默认值是1000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM