2
我用下面的抽象類型在我的symfony 2的應用程序,以修改相冊集合形式的主題化的Symfony2
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text')
->add('description', 'textarea')
->add('public', 'checkbox')
->add('lists', 'collection', array(
'attr' => array('data-category' => 'access-right'),
'type' => 'albumListType',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
)
)
->add('medias', 'collection', array(
'attr' => array('data-category' => 'media'),
'type' => new MediaType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
)
)
;
}
當我顯示這種形式,我需要以顯示與文章鏈接各媒體的縮略圖等等 我嘗試過載mediawidget以產生以下HTML
<div class="row" id="albumtype_media" data-prototype="prototype">
<div class="col-md-3" id="albumtype_media_0">
<img src="path"/>
<input type="hidden" name="albymtype_media[0][path]" value="path"/>
</div>
<div class="col-md-3" id="albumtype_media_1">
<img src="path"/>
<input type="hidden" name="albymtype_media[1][path]" value="path"/>
</div>
</div>
爲了實現這一點,我用一種特殊形式的主題來定製媒體插件
{% block _medias_widget %}
{% spaceless %}
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
{% endif %}
<div class="row" {{ block('widget_container_attributes') }}>
{{ block('collection_media_rows') }}
{{ form_rest(form) }}
</div>
{% endspaceless %}
{% endblock %}
{% block collection_media_rows %}
{% spaceless %}
{{ form_errors(form) }}
{% for innerform in form %}
{% spaceless %}
<div class="col-md-3" {{ block('widget_attributes') }}>
{% if form.parent is empty %}
{{ form_errors(innerform) }}
{% endif %}
<img src="{{ innerform.vars.value.path }}"/>
{% for child in innerform %}
{{ form_errors(child) }}
{{ form_label(child) }}
{{ form_widget(child) }}
{% endfor %}
</div>
{% endspaceless %}
{% endfor %}
{% endspaceless %}
{% endblock %}
wichh給我下面的輸出
<div class="row" id="albumtype_media" data-prototype="prototype">
<div class="col-md-3" id="albumtype_media" data-prototype="prototype">
<img src="path"/>
<input type="hidden" name="albymtype_media[0][path]" value="path"/>
</div>
<div class="col-md-3" id="albumtype_media" data-prototype="prototype">
<div id="albumtype_media_1">
<input type="hidden" name="albymtype_media[1][path]" value="path"/>
</div>
</div>
我用symfony 2開始我不會在樹枝有一個很好的理解,並尋找爲form_widget的源代碼並不能幫助我既不是如此如果你能解釋我如何得到理想的結果,我會非常感激。
您可以嘗試使用symfony [form type extension](http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html#加入最伸長業務邏輯) – xiidea