2015-09-22 103 views
1

我遵循官方文檔示例,但無法使其正常工作。嵌入表單的集合顯示整個表單的字段

總結,我有材料items_budget表。他們兩個是相關的一對多和多對一,由idmaterial領域:

Material.orm.yml

oneToMany: 
    itemsBudget: 
     targetEntity: ItemsBudget 
     mappedBy: material 

ItemsBudget.orm.yml

material: 
    targetEntity: Material 
    inversedBy: itemsBudget 
    joinColumn: 
     name: material 
     referencedColumnName: id 

這裏,ItemsBudgetType哪裏我設置了material字段作爲collection

$builder->add('budget', 'entity', array(
      'class' => 'PanelBundle:Budget', 
      'attr' => array(
       'class' => 'form-control', 
      ), 
     )) 
     ->add('material', 'collection', array(
      'type' => new MaterialType(), 
      'allow_add' => true 
     )) 
     ->add('quantity', 'number', array(
      'attr' => array(
       'class' => 'form-control', 
      ), 
     )) 
     ->add('price', 'money', array(
      'attr' => array(
       'class' => 'form-control', 
      ), 
     )); 

只是供參考的MaterialType

$builder->add('name', 'text', array(
      'attr' => array(
       'class' => 'form-control', 
      ), 
     )) 
     ->add('description', 'text', array(
      'attr' => array(
       'class' => 'form-control', 
      ), 
     )) 
     ->add('current_quantity', 'text', array(
      'attr' => array(
       'class' => 'form-control', 
       'required' => false 
      ), 
     )) 
     ->add('price', 'money', array(
      'attr' => array(
       'class' => 'form-control', 
      ), 
     )); 

在這裏,我ItemsBudget鑑於index.html.twig

<strong>Materiais</strong> 
<div class="form-group"> 
    <ul class="materials" data-prototype="{{ form_widget(form.material.vars.prototype)|e }}"> 
     {% for material in form.material %} 
      <li>{{ form_widget(form.material.vars.prototype.name) }}</li> 
     {% endfor %} 
    </ul> 
</div> 

在視圖中,我也嘗試過,因爲它是在th例如:{{ form_row(material.name) }},但它仍然顯示整個Material窗體。

在哪裏,我打電話給他們,在ItemsBudgetController

public function addAction(Request $request) 
{ 
    $form = $this->createForm(new ItemsBudgetType(), new ItemsBudget()); 
    $manager = $this->getDoctrine()->getManager(); 

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

     if ($form->isValid()) { 
      $ItemsBudgetEntity = $form->getData(); 
      $manager->persist($ItemsBudgetEntity); 
      $manager->flush(); 

      $BudgetEntity = $form->get('budget')->getData(); 
      $BudgetEntity->addItemsBudget($ItemsBudgetEntity); 

      $manager->persist($BudgetEntity); 
      $manager->flush(); 

      $this->addFlash('success', 'Materiais para o orçamento adicionados'); 

      return $this->redirect($this->generateUrl('panel_budgets')); 
     } 
    } 

    return $this->render('PanelBundle:ItemsBudget:index.html.twig', array(
     'form' => $form->createView(), 
     'budgets' => $manager->getRepository('PanelBundle:Budget')->findAll() 
    )); 
} 

的JavaScript是由太的例子一樣:

function addMaterial($collectionHolder, $newLinkLi) { 
    var prototype = $collectionHolder.data('prototype'); 
    var index = $collectionHolder.data('index'); 
    var newForm = prototype.replace(/__name__/g, index); 

    $collectionHolder.data('index', index + 1); 

    var $newFormLi = $('<li></li>').append(newForm); 
    $newLinkLi.before($newFormLi); 
} 

var $collectionHolder; 
var addMaterialLink = $('<a href="#" class="add_material_link">Mais</a>'); 
var $newLinkLi = $('<li></li>').append(addMaterialLink); 

jQuery(document).ready(function() { 
    $collectionHolder = $('ul.materials'); 
    $collectionHolder.append($newLinkLi); 
    $collectionHolder.data('index', $collectionHolder.find(':input').length); 

    addMaterialLink.on('click', function (e) { 
     e.preventDefault(); 
     addMaterial($collectionHolder, $newLinkLi); 
    }); 
}); 

這是問題:不是顯示的從Material的形式只有name字段,它是每次單擊「Mais」時顯示整個表單。我錯過了什麼嗎?

+0

您所期望的,當你在'數據prototype'屬性整個形式? – malcolm

回答

1

JavaScript將獲取data-prototype屬性的內容並將其附加到表單的html中。模板中的屬性包含{{ form_widget(form.material.vars.prototype)|e }},它是表單的html原型。

嘗試:

<ul class="materials" data-prototype="{{ form_widget(form.material.vars.prototype.name) }}"> 
+0

哦,!@#$。現在我明白了文檔。顯示它的例子。我以爲我不得不放入'for'。謝謝@LorenzSchaef。 :) – mfgabriel92