2012-11-14 53 views
0

我在枝條中呈現表單時遇到問題。我試圖嵌入一組表單。當我呈現集合時,它不顯示,而顯示錶單的名稱。目的是使用添加按鈕在表單中添加表單,以便在運行時添加集合中每個元素的表單。我看看Symfony Docs,我想我一步步跟着它。如何渲染表單集合

這是我的控制器:

function new_resultAction($id) 
{ 
    $em = $this->getDoctrine()->getEntityManager(); 

    $test = $em->getRepository('LadelaOdeskTesterBundle:Test')->find($id); 
    $categories = 
     $em->getRepository('LadelaOdeskTesterBundle:Category')->findByTest($test); 
    if (!$test) { 
     throw $this->createNotFoundException('Unable to find Test entity.'); 
    } 

    $resultCategory = new Category(); 


    $form = $this->createForm(new CategoryType() , $resultCategory); 

    $request = $this->getRequest(); 

    if ('POST' === $request->getMethod()) { 

     $form->bindRequest($request); 

     if ($form->isValid()) { 

      $em->persist($resultCategory);     
      $em->flush(); 

      $this->get('session')->setFlash('success', 'New result Categories were 
      saved!'); 
      return $this->redirect($this->generateUrl('questions', array(
       'id' => $resultCategory->getId(), 
      ))); 
     } 
    }     
    return array(
     'test' => $test, 
     'categories' =>$categories, 

     'form' => $form->createView(), 


    ); 

} 

我的形式:

class ResultCategoryType extends AbstractType 
    { 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
    $builder->add('value', 'integer'); 
    $builder->add('category', 'entity', array(
      'class'   => 'Ladela\OdeskTesterBundle\Entity\Category', 
      'query_builder' => function ($repository) { return 
    $repository->createQueryBuilder('p')->orderBy('p.name', 'ASC'); }, 
      'property' => 'name' , 
       'expanded' => false , 
       'multiple' => false ,)); 


} 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
    $resolver->setDefaults(array(
     'data_class' => 'Ladela\OdeskTesterBundle\Entity\ResultCategory' 
    )); 
    } 

    public function getName() 
{ 
    return 'ResultCategory'; 
} 
} 

class CategoryType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('name') 
     ->add('resultCategories','collection', array(
      'type' => new ResultCategoryType(), 
      'allow_add' => true, 
      'by_reference' => false, 
      'prototype' => true, 

     )) 
    ; 
} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Ladela\OdeskTesterBundle\Entity\Category' 
    )); 
} 

public function getName() 
{ 
    return 'category'; 
} 

}

我的樹枝文件:

<form method="post" action="{{ path('questions',{'id': test.id }) }}" {{ 
    form_enctype(form) }} >  

    <ul class="tags" data-prototype="{{ 
    form_widget(form.resultCategories.vars.prototype)|e 
    }}"> 
    {# iterate over each existing tag and render its only field: name #} 
    {% for ResultCategory in form.resultCategories %} 
     <li>{{ form_row(ResultCategory.category) }}</li> 
    {% endfor %} 
<a href="#" class="add_tag_link">Add a tag</a> 

</ul>   
{{ form_rest(form) }}{# form.items's prototype is rendered twice #} 
{{ form_errors(form) }} 
<input type="submit" value ="add" /> 
</form> 

{% block javascripts %} 


<script type="text/javascript"> 
// Get the div that holds the collection of tags 
var collectionHolder = $('ul.tags'); 

// setup an "add a tag" link 
    var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>'); 
    var $newLinkLi = $('<li></li>').append($addTagLink); 

jQuery(document).ready(function() { 
// add the "add a tag" anchor and li to the tags ul 
collectionHolder.append($newLinkLi); 

$addTagLink.on('click', function(e) { 
    // prevent the link from creating a "#" on the URL 
    e.preventDefault(); 

    // add a new tag form (see next code block) 
    addTagForm(collectionHolder, $newLinkLi); 
    }); 
}); 
    // Get the div that holds the collection of tags 
var collectionHolder = $('ul.tags'); 

    // setup an "add a tag" link 
var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>'); 
var $newLinkLi = $('<li></li>').append($addTagLink); 

    jQuery(document).ready(function() { 
// add the "add a tag" anchor and li to the tags ul 
    collectionHolder.append($newLinkLi); 

$addTagLink.on('click', function(e) { 
    // prevent the link from creating a "#" on the URL 
    e.preventDefault(); 

    // add a new tag form (see next code block) 
    addTagForm(collectionHolder, $newLinkLi); 
    }); 
    }); 

    {% endblock %} 

回答

2

的形式收集不呈現因爲你沒有發起它! See the official docs。有用於創建標籤表單的「虛擬」標籤對象。

+0

我必須把這樣的行代碼像虛標籤? –

+0

目標是爲每個要添加的標籤顯示一個「TagType」表單!如果您在創建表單時未傳遞標記對象,則不會呈現任何表單!您可以使用「添加」按鈕在運行時通過使用原型創建一個新的標記形式 – JeanValjean

+0

好吧,我會嘗試現在:),謝謝 –