序列化FormView控件的数据JSON [英] Serializing FormView data in JSON

查看:113
本文介绍了序列化FormView控件的数据JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变呈现模板的方法。我使用的是服务器端模板引擎。现在,我需要从后端,而不是HTML只返回JSON,我需要将我的模板到前端。

I am trying to change the approach of rendering templates. I was using a server-side template engine. Now that I need to return only JSON from backend instead of HTML, I need to move my templating to front-end.

我的问题作为一个新手,是当模板中包含的一种形式。换句话说,当最终的JSON还应包含一个CSRF令牌。请采取CRUD应用程序书籍的例子(一个实体图书(id_book,姓名))。为读取模板荷兰国际集团的记录显示了书名,并允许删除一本书。

The problem for me as a newbie, is when the template contains a form. In other words, when the final JSON should also contain a csrf token. Please to take the example of CRUD application for books (one entity Book(id_book, name)). The template for Reading a record shows the book name and allow deletion of a book.

在Symfony2的控制器中,我使用 createFormBuilder()方法创建删除表单对象,以我所适用的 CreateView的()方法。由后者返回的对象是在模板引擎(小枝)使用 form_widget()。在结束:

In Symfony2 controller, I use createFormBuilder() method that creates the delete form object, to which I apply createView()method. The object returned by the latter is used by form_widget()in the template engine (Twig). At the end:

<div id="bookName">{{book.name}}</div>
<div id="bookDelete">
      <form action="{{ path('book_delete', { 'id': book.id }) }}" method="post">
          <input type="hidden" name="_method" value="DELETE" />
          {{ form_widget(delete_form) }}
          <button type="submit"></button>
      </form>
</div>

这将返回:

<div id="bookName">Symfony2 In Depth</div>
<div id="bookDelete">
    <form action="/web/app_dev.php/deletes" method="post">
         <input type="hidden" name="_method" value="DELETE">
         <div id="form">
             <input type="hidden" id="form_id" name="form[id]" value="15">
             <input type="hidden" id="form__token" name="form[_token]" value="dd6573ae916ae30f78ba35a8c67e5d42a2764c1c">
        </div>
        <button type="submit"></button>
    </form>

我想象从服务器移动时,模板渲染到前端是一个最终的JSON看起来像:

What I imagine when moving template rendering to front-end is a final JSON from server looking like:

{
'id':15,
'name': 'Symfony2 in Depth',
'csrf_token' : 'dd6573ae916ae30f78ba35a8c67e5d42a2764c1c'    
}

问题是如何实现相同的Symfony2的内部机制来呈现CSRF令牌DELETE的形式,作为最终JSON的一部分读一本书的实体?难道是可以接受的摆脱的{{form_widget(delete_form)}} 及其所有长的物体,只有序列化与图书名称CSRF令牌?这是什么会影响?我觉得这是很好的性能,但是怎么样?

Question is how to implement the same Symfony2 internal mechanism to render csrf token for DELETE form, as part of the final JSON to read a book entity? Is it acceptable to get rid of {{ form_widget(delete_form) }} and all its long objects, and only serialize csrf token with book name? What does this will affect? I feel it is good for performance but how?

您平常的指导多AP preciated。

Your usual guidance is much appreciated.

推荐答案

您可以创建你枝杈文件是这样的:

You can create you twig file something like:

{
    'id': '{{ form.id.vars.value }}'
    'name': '{{ form.name.vars.value }}'
    'csrf_token': '{{ form._token.vars.value }}'
}

反正我不建议你使用CSRF令牌,当您使用的API,它是更好,如果你禁用。如果要禁用对config.yml所有的应用程序:

Anyway I dont recommend you use csrf token when you are using API, it is better if you disabled. If you want to disable for all application in the config.yml:

framework:
    csrf_protection:
        enabled:        false

或只是在类型表单中添加一种形式:

Or just for one form in the Type form add:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'csrf_protection' => false,
    ));
}

这篇关于序列化FormView控件的数据JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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