如何自定义Symfony 2表单中的数据原型属性 [英] How to customize the data-prototype attribute in Symfony 2 forms

查看:127
本文介绍了如何自定义Symfony 2表单中的数据原型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于umpteens天,我阻止Symfony 2和表单的问题。

Since umpteens days, I block on a problem with Symfony 2 and forms.

我有一种形式的网站实体。 网站是网站实体的集合,每个网站包含两个属性:类型和url。

I got a form of websites entities. "Websites" is a collection of website's entities and each website contains two attributes : "type" and "url".

如果我想在我的网站中添加更多的网站数据库,我可以点击添加另一个网站链接,其中添加另一个网站行到我的形式。因此,当您点击提交按钮时,您可以同时添加一个或X网站。

If I want to add more of one website in my database, I can click on a "Add another website" link, which add another website row to my form. So when you click on the submit button, you can add one or X website(s) at the same time.

此过程添加一行使用数据原型属性,它可以生成网站子表单。

This process to add a row use the data-prototype attribute, which can generate the website sub-form.

问题是我自定义我的表单有一个伟大的图形呈现...像:

The problem is that I customize my form to have a great graphic rendering... like that :

<div class="informations_widget">{{ form_widget(website.type.code) }}</div>
<div class="informations_error">{{ form_errors(website.type) }}</div>
<div class="informations_widget">{{ form_widget(website.url) }}</div>
<div class="informations_error">{{ form_errors(website.url) }}</div>

但是数据原型不关心这个自定义,HTML和CSS标签&属性。我保持Symfony渲染:

But the data-prototype doesn't care about this customization, with HTML and CSS tags & properties. I keep the Symfony rendering :

<div>
<label class=" required">$$name$$</label>
<div id="jobcast_profilebundle_websitestype_websites_$$name$$">
<div>
<label class=" required">Type</label>
<div id="jobcast_profilebundle_websitestype_websites_$$name$$_type">
<div>
<label for="jobcast_profilebundle_websitestype_websites_$$name$$_type_code" class=" required">label</label>
<select id="jobcast_profilebundle_websitestype_websites_$$name$$_type_code" name="jobcast_profilebundle_websitestype[websites][$$name$$][type][code]" required="required">
<option value="WEB-OTHER">Autre</option>
<option value="WEB-RSS">Flux RSS</option>
...
</select>
</div>
</div>
</div>
<div>
<label for="jobcast_profilebundle_websitestype_websites_$$name$$_url" class=" required">Adresse</label>
<input  type="url" id="jobcast_profilebundle_websitestype_websites_$$name$$_url" name="jobcast_profilebundle_websitestype[websites][$$name$$][url]" required="required" value="" />
</div>
</div>
</div>

有没有人有想法来做这个黑客?

Does anyone have an idea to make that hack ?

推荐答案

有点旧,但这里是一个致命的简单解决方案。

A bit old, but here is a deadly simple solution.

通过一个Twig模板,所以你有足够的能力定制原型,将放置在您的 data-prototype =...标记。

The idea is simply to render the collection items through a Twig template, so you have full ability to customize the prototype that will be placed in your data-prototype="..." tag. Just as if it was a normal, usual form.

在YourMainForm.html.twig:

<div id="collectionContainer"
     data-prototype="
         {% filter escape %}
             {{ include('MyBundle:MyViewsDirectory:prototype.html.twig', { 'form': form.myForm.vars.prototype }) }}
         {% endfilter %}">
</div>

在MyBundle:MyViewsDirectory:prototype.html.twig: p>

And in MyBundle:MyViewsDirectory:prototype.html.twig:

<div>
    <!-- customize as you wish -->
    {{ form_label(form.field1) }}
    {{ form_widget(form.field1) }}
    {{ form_label(form.field2) }}
    {{ form_widget(form.field2) }}
</div>

信用:改编自https://gist.github.com/tobalgists/4032213

这篇关于如何自定义Symfony 2表单中的数据原型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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