简体   繁体   English

仅自定义Symfony表单的数组元素/片段

[英]Customise an array element/fragment of a Symfony form only

I am attempting to customise the markup of a form element in my Symfony form, however I am having difficulty targeting this element alone. 我正在尝试在Symfony表单中自定义表单元素的标记,但是我很难仅针对该元素。 I have read this section in the SF cookbook - but I am having difficulty applying it to my situation. 我已经在SF食谱中阅读了本节 -但在将其应用于我的情况时遇到了困难。

The form_row block that I have extended does what I want it to do, however this applies to all form_row s called in that template - how can I apply it to sizes (which is an array) only ? form_row我已经扩展块做什么,我想要它做的,然而,这适用于所有form_row这叫在模板-我该如何将它应用到sizes 只有 (这是一个数组)?

view.html.twig: view.html.twig:

{% form_theme form 'CRMPiccoProductBundle:CourseGuideRow:fields.html.twig' %}

{{ form_row(form.name) }}
{% for size in form.sizes %}
   {{ form_row(size) }}
{% endfor %}

fields.html.twig: fields.html.twig:

{% block form_row %}
    <td style="width: 75px; border-right: 2px solid #000000;">
        {{ form_widget(form) }}
    </td>
{% endblock %}

My form is called crmpicco_course_guide_row : 我的表单称为crmpicco_course_guide_row

/**
 * @return string name
 */
public function getName()
{
    return 'crmpicco_course_guide_row';
}

This results in this markup: 这将导致以下标记:

    <td style="width: 75px; border-right: 2px solid #000000;">
    <input type="text" class=" form-control" 
required="required" name="crmpicco_course_guide_row[sizes][0]" 
id="crmpicco_course_guide_row_sizes_0">
    </td>

我认为您只需要将表单主题仅应用于“大小”子表单。

{% form_theme form.sizes 'CRMPiccoProductBundle:CourseGuideRow:fields.html.twig' %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM