8
我想創建一個包含嵌套集合的表單。 我不知道如何處理JS部分來顯示子集合。有誰知道我該怎麼做?Symfony 2:如何處理表單中的嵌套集合
這裏是我的形式代碼:
class ParentFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('case', 'choice', array(
'choices' => array(
'case1' => 'case1',
'case2' => 'case2',
'case3' => 'case3',
)))
->add ('subForm1', 'collection', array (
'type' => new Sub1FormType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
))
;
$builder->add('save',"submit") ;
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
}
public function getName() {
return 'formtestparenttype';
}
}
class Sub1FormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('fieldSub1',"text")
->add ('childForm1', 'collection', array (
'type' => new Sub2FormType,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
}
public function getName() {
return 'formtestsub1type';
}
}
class Sub2FormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('fieldSub2',"text")
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
}
public function getName() {
return 'formtesttype';
}
}
控制器:
$form = $this->createForm(new ParentFormType()) ;
return $this->render('MyBundle:Test:test.html.twig', array(
'form' => $form->createView()
));
這裏;小枝+ js部分:
{% extends '::base.html.twig' %}
{% block content %}
{{ form_start(form) }}
<h3>Tags</h3>
<ul class="collectionHolder" data-prototype="{{ form_widget(form.subForm1.vars.prototype)|e }}">
{# iterate over each existing tag and render its only field: name #}
{% for subForm1 in form.subForm1 %}
<li>{{ form_row(subForm1) }} </li>
<ul class="collectionHolder" data-prototype="{{ form_widget(subForm2.vars.prototype)|e }}">
{%for subForm2 in subForm1.subForm2 %}
<li>{{ form_row(subForm2) }}</li>
{% endfor %}
{% endfor %}
</ul>
{{ form_end(form) }}
<script>
var $collectionHolder;
// setup an "add a tag" linkd
var $addTagLink = $('<a href="#" class="add_tag_link">Add</a>');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
function addTagForm($collectionHolder, $newLinkLi)
{
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
// get the new index
var index = $collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$collectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}
$collectionHolder = $('.collectionHolder');
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addTagLink.on('click', function(e) {
e.preventDefault();
addTagForm($collectionHolder, $newLinkLi);
});
});
</script>
{% endblock content %}
你能解釋一下你的問題到底是什麼嗎?從代碼的外觀來看,您已經從Symfony文檔訪問過Forms Collection頁面。 – ecc 2015-04-16 09:29:52