Aus*_*ams 2 forms symfony twig
我有一个表单,可以根据我的数据库中的实体动态生成字段(我的实体是课程).对于每个新课程,它会添加一个表单字段来修改它的排序顺序.我的问题是,如何在我的课程表中动态显示这些单独的表单字段?
我的形式逻辑:
foreach ($entities as $id => $course) { //$id key included to show you courses key value
$formBuilder->add($course->getId(), 'text', array(
'data' => $course->getsortOrder(),
'label' => false,
'attr' => array('class' => 'sort' /*, 'style' => 'visibility:hidden;'*/ )
));
}
Run Code Online (Sandbox Code Playgroud)
我的jQuery修改了表单字段:
$(document).ready(function () {
$(".records_list tbody").sortable({items: "tr"},
{stop: function(event, ui) {
$(".sort").each(function(index) {
$(this).val(index);
});
}
});
});
/* I tested the proper functionality of this jQuery by putting
<input type="text" class="sort" value="{{ entity.sortOrder }}">
into the <td> that sort order is in. I want to replace this with
something like {{ form_widget(form.{entity.id}) }} */
Run Code Online (Sandbox Code Playgroud)
我可以很容易地说:
{% for entity in entities %}
<td>
{% if( entity.id == 1) %}
{{form_widget(form.1)}} //1 is entity id
{% else if (entity.id == 2 ) %}
{{form_widget(form.2)}} //2 is entity id
{% else if (entity.id == 3 ) %}
{{form_widget(form.3)}} //3 is entity id
{% endif %}
</td>
{% enfor %}
Run Code Online (Sandbox Code Playgroud)
但这显然不是很有活力.你添加一个新课程,它会中断.
如果我能说,那就太好了
{% for entity in entities %}
<td>
{% set course = entity.id %}
{{form_widget(form.course)}}
</td>
{% endfor%}
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,这不起作用.
任何关于如何动态地将这些表单字段添加到我的sortOrder中的见解表示赞赏.
我不确定你想要做什么.
但是,如果要将属性动态访问到对象或数组,则可以使用属性Twig函数.
你应该在模板中尝试这样的事情:
{% for entity in entities %}
<td>
{{ form_widget(attribute(form, entity.id)) }}
</td>
{% endfor%}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2833 次 |
| 最近记录: |