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

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

问题描述

很多天以来,我一直在阻止 Symfony 2 和表单的问题.

我得到了一种形式的网站实体.网站"是网站实体的集合,每个网站包含两个属性:类型"和网址".

如果我想在我的数据库中添加更多一个网站,我可以点击添加另一个网站"链接,这会在我的表单中添加另一个网站行.因此,当您点击提交按钮时,您可以同时添加一个或 X 个网站.

这个添加行的过程使用了data-prototype属性,可以生成网站子表单.

问题是我自定义了我的表单以获得很好的图形渲染......就像那样:

{{ form_widget(website.type.code) }}

<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 渲染:

<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>...</选择>

<div><label for="jobcast_profilebundle_websitestype_websites_$$name$$_url" class=" required">地址</label><input type="url" id="jobcast_profilebundle_websitestype_websites_$$name$$_url" name="jobcast_profilebundle_websitestype[websites][$$name$$][url]" required="required" value=""/>

有没有人有制作这个黑客的想法?

解决方案

有点旧,但这里有一个非常简单的解决方案.

这个想法只是通过 Twig 模板呈现集合项,因此您可以完全自定义将放置在 data-prototype="..." 标签中的原型.就好像它是一种正常的、通常的形式一样.

在你的MainForm.html.twig中:

在 MyBundle:MyViewsDirectory:prototype.html.twig:

<!-- 随心所欲地定制-->{{ form_label(form.field1) }}{{ form_widget(form.field1) }}{{ form_label(form.field2) }}{{ form_widget(form.field2) }}

来源:改编自 https://gist.github.com/tobalgists/4032213

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

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

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>

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.

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.

In yourMainForm.html.twig:

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

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>

Credit: adapted from https://gist.github.com/tobalgists/4032213

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆