Symfony2:具有数据原型的集合表单字段类型 [英] Symfony2: collection form field type with data-prototype

查看:28
本文介绍了Symfony2:具有数据原型的集合表单字段类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个团队,我想向其中添加玩家(玩家的对象).我已经正确设置了表单类型"类.这是我观点的相关部分:

{% for index, player in form.players %}<div id="template" data-prototype="{{ form_widget(form.players.get('prototype')) | e }}"><span class="title">{{ index }}</span>{{ form_row(player.name) }}

{% 结束为 %}

我的问题是 data-prototype 属性不包含 span 标签;它只包含 {{ form_row(player.name) }} 的输出.有没有办法在 data-prototype 属性中包含 div#template 的全部内容?

解决方案

这也困扰着我.我自定义了一个字段类型,甚至用减少的字段集制作了简单的表单类型(对于更复杂的对象),但感觉不太对.我想出了一个可以解决问题的解决方案:)

首先 - 我认为您的 <div id="team" data-prototype="..."> 应该在您的循环之外.

接下来,您的原型只是一个表单视图对象,因此如果您将其传递给模板,您可以手动渲染字段并自定义输出.注意:自动转义对包含的内容没有影响,因此请使用过滤器转义".

<div id="playerFields" data-prototype="{% filter escape %}{% include 'AcmeTeamBundle:Team:prototypePlayer.html.twig' with {'form': form.players.get('prototype')} %}{% endfilter %}">

然后在你的prototypePlayer.html.twig中,像往常一样在表单视图中渲染每个字段.

<span class="title">{{ form_label(form.name) }}</span>{{ form_row(form.name) }}<span class="age">{{ form_label(form.age) }}</span>{{ form_row(form.age) }}{# 从你的 Form/PlayerType 类中渲染任何你喜欢的东西...... #}<div>

我的模板与您的问题有不同的数据结构,但应该会有所帮助:)

干杯,

克里斯

<小时>

弃用警告:

从 Symfony 2.1 开始,.get() 已被弃用,上述方法将不起作用.将 form.players.get('prototype') 替换为 form.players.vars.prototype 以在更高版本的 Symfony 中修复此问题.

I have a Team to which I would like to add players (objects of Player). I've set up the form "type" classes correctly. Here's the relevant part of my view:

{% for index, player in form.players %}
    <div id="template" data-prototype="{{ form_widget(form.players.get('prototype')) | e }}">
        <span class="title">{{ index }}</span>
        {{ form_row(player.name) }}
    </div>
{% endfor %}

My problem is that data-prototype attribute doesn't include the span tag; it only contains the output of {{ form_row(player.name) }}. Is there a way to include the whole content of div#template in the data-prototype attribute?

解决方案

This was bugging me too. I customized a field type and even made simple form types with a reduced fieldset (for more complex objects), but it just didn't feel right. I've come up with a solution that should do the trick :)

First - I think your <div id="team" data-prototype="..."> should be outside your loop.

Next, your prototype is just a form view object, so if you pass it to a template you can render fields by hand and customize the output. Note: autoescape has no effect on included content, so use 'filter escape'.

<div id="playerFields" data-prototype="{% filter escape %}{% include 'AcmeTeamBundle:Team:prototypePlayer.html.twig' with {'form': form.players.get('prototype')} %}{% endfilter %}">

Then in your prototypePlayer.html.twig, just render each field as you normally would in a form view.

<div>
    <span class="title">{{ form_label(form.name) }}</span>
    {{ form_row(form.name) }}
    <span class="age">{{ form_label(form.age) }}</span>
    {{ form_row(form.age) }}
    {# render whatever else you like from your Form/PlayerType class... #}
<div>

My template has a different data structure to your question, but it should help :)

Cheers,

Chris


Deprecation Warning:

As of Symfony 2.1, .get() is deprecated and the above will not work. Replace form.players.get('prototype') with form.players.vars.prototype to fix this in later versions of Symfony.

这篇关于Symfony2:具有数据原型的集合表单字段类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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