自定义集合表单字段中的表单行

时间:2015-01-02 18:16:10

标签: php forms symfony twig

我正在尝试在表单布局中自定义特定的表单行。从symfony cookbook它告诉我,如果我理解正确,我可以通过在表单主题文件中添加代码块来修改表单。

我将字段添加到表单中:

->add('editions', 'collection', 
    array('type' => new EditionType(), 
        'allow_add' => true, 
        'allow_delete' => true,
        'by_reference' => false,
    )
)

我按照以下方式在模板中生成表单行,我需要data-prototype标签,因为用户需要在查看表单时添加版本:

<div class="editions" data-prototype="{{ form_widget(form.editions.vars.prototype)|e }}"> 
{% for edition in form.editions %}
    <section class="edition-container">
        {{ form_label(edition.isbn) }}
        {{ form_errors(edition.isbn) }}
        {{ form_widget(edition.isbn) }}
    </section>
{% endfor %}
</div>

要自定义“版本”字段的行,我尝试了以下操作:

<!-- Custom form theme for textarea label rows -->
{% block textarea_label %}
    {% spaceless %}
    <div {{ block('label_container_attributes') }}>
        <label class="label">{{ label }}</label>
    </div>
    {% endspaceless %}
{% endblock %}

<!-- Custom form theme for edition field textarea label rows -->
{% block _booklist_editions_label %}
    {% spaceless %}
    <div {{ block('label_container_attributes') }}>
        <label class="label">{{ label }}!</label>
    </div>
    {% endspaceless %}
{% endblock %}

不幸的是第二个块没有工作,但第一个块确实有效。我注意到的是,editions字段中的外部div没有id属性,其中包含字段名称。

这让我觉得问题的解决方案可能就是其中之一:

  1. 自定义表单主题块名称错误,不知道我应该给出什么其他名称。
  2. id不会添加到外部div中,因此它不会“链接”自定义表单样式。
  3. 有人可以解释我如何使用外部表单主题文件向我的“版本”字段添加自定义表单主题,或者我如何以其他方式实现此目的。

0 个答案:

没有答案
相关问题