Symfony 2-删除表格和CSRF令牌 [英] Symfony 2 - Delete Forms and CSRF Token

查看:74
本文介绍了Symfony 2-删除表格和CSRF令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自数据库的条目列表。我想在每一行的末尾都有一个删除按钮,这样用户就不必先进入编辑/显示页面来删除条目。

I have a List of entries coming from a database. I would like to have a "Delete-Button" at the end of every row, so that the user won't have to first go to the edit/show page to delete the entry.

我尝试使用csrf令牌创建隐藏的输入字段,如下所示:

I tried creating a hidden input field with the csrf token like so:

return $this->createFormBuilder()
   ->getForm()
;

这将输出:

<div id="form">
   <input type="hidden" id="form__token" name="form[_token]" value="6c98ebfa9df07.....">
</div>

我将其余的表格放在树枝模板中,以便每个表格都有自己的动作路径

The rest of the Form i put around in the twig template so that every form has its own action path according the the id of the entry.

不幸的是,在树枝模板中,只有第一个

unfortunately in the twig template only the first

{{ form_widget(delete_form) }}

将被渲染。

我如何更频繁地使用此隐藏字段?
或有什么方法可以完全不同地做这件事吗?

How can i use this hidden field more often? OR is there any way to do this whole thing differently?

感谢您的帮助

public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $deleteForm = $this->createDeleteForms();

        $entities = $em->getRepository('IntranetServicesBundle:Laender')->findAll();

        return $this->render('IntranetServicesBundle:Laender:index.html.twig', array(
            'entities' => $entities,
            'delete_form' => $deleteForm->createView(),
        ));
    }


private function createDeleteForms()
{
    return $this->createFormBuilder()
        ->add('id', 'hidden')
        ->getForm()
    ;
}


推荐答案

@Lighthart的答案我给出正确的答案:

The answer of @Lighthart led me to the correct answer:

在您的控制器中生成一系列表单视图并将其移至视图:

In your controller generate an array of form views and had it over to the view:

public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('AppBundle:Entity')->findAll();

    $delete_forms = array_map(
        function ($element) {
            return $this->createDeleteForm($element->getId())->createView();
        }
        , $entities
    );

    return $this->render('AppBundle:Entity:index.html.twig', array(
        'entities' => $entities,
        'delete_forms' => $delete_forms
    ));
}

现在,您必须在视图中访问它。因此,您可以使用表单函数和特殊的循环变量:

Now you have to access this in your view. Therefore you can use the form functions and the special loop variables:

{% extends '::base.html.twig' %}

{% block body %}
    {% for entity in entities %}
        {# Access the delete form for this entity using the loop index ... #}
        {% set delete_form = delete_forms[loop.index0] %}

        {# ... and display the form using the form tags. #}
        {{ form_start(delete_form) }}
            <input type="submit" value="Delete" />
        {{ form_end(delete_form) }}
    {% endfor %}
{% endblock %}

就是这样。

这篇关于Symfony 2-删除表格和CSRF令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆