Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

php - Symfony2: collection form field type with data-prototype

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...