2013-02-25 77 views
0

我在頁面上看到一個實體的多個字段,我希望每個字段都可以通過ajax編輯,每次只能編輯一個字段。 要做到這一點,我提出了爲所有領域構建獨特控制器的想法,但我無法使其工作,我不知道它是否是我嘗試做的正確解決方案。 我的網頁秀場:Symfony2動態創建字段實體

<div> 
<form class="ajax" action="{{ path('ajax_setSocial', { 'id': entity.id }) }}" method="post" {{ form_enctype(form) }}> 
    <div class="editor"> 
     {{ form_errors(form) }} 
     <div class="editLabel pls lti">{{ form_label(form.ragSocial) }}</div> 
     <div class="editField"> 
      <div class="ptm"> 
       {{ form_widget(form.ragSocial) }} {{ form_errors(form.ragSocial) }} 
      </div>  
      {{ form_widget(form._token) }} 
      <div class="mtm"> 
       <button class="btn btn-primary disabled save" type="submit" disabled>Save</button> 
       <button class="btn ann">Cancel</button> 
      </div> 
     </div> 
    </div> 
    </form> 
</div> 
<div> 
    <form class="ajax" action="{{ path('ajax_setSocial', { 'id': entity.id }) }}" method="post" {{ form_enctype(form) }}> 
    <div class="editor"> 
     {{ form_errors(form) }} 
     <div class="editLabel pls lti">{{ form_label(form.pIva) }}</div> 
     <div class="editField"> 
      <div class="ptm"> 
       {{ form_widget(form.pIva) }} {{ form_errors(form.pIva) }} 
      </div>  
      {{ form_widget(form._token) }} 
      <div class="mtm"> 
       <button class="btn btn-primary disabled save" type="submit" disabled>Save</button> 
       <button class="btn ann">Cancel</button> 
      </div> 
     </div> 
    </div> 
    </form> 
</div> 
在我的控制器

public function setSocialAction(Request $request, $id) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $entity = $em->getRepository('MyBusinessBundle:Anagrafica')->find($id); 

    if (!$entity) { 
     throw $this->createNotFoundException('Unable to find Anagrafic entity.'); 
    } 
    $field = $request->get('field'); 
    $class = $field.'Type()'; 
    $form = $this->createForm(new $class, $entity); 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em->persist($entity); 
     $em->flush(); 

     $response = new Response(); 
     $output = array('success' => true); 
     $response->headers->set('Content-Type', 'application/json'); 
     $response->setContent(json_encode($output)); 

     return $response; 
    } 

$類= $ field.'Type()「; $ form = $ this-> createForm(new $ class,$ entity); 用幾行代碼我儘量讓生成的表單字段,但不工作,因爲它正在轉變爲一個字符串,我得到的錯誤是動態類:

Fatal error: Class 'ragSocialType()' not found 

但類是!並且也被稱爲頂部文件。 我希望我解釋過,我接受任何建議,以更好的方式!

+0

請發佈您的ragSocialType – Lighthart 2013-02-25 05:15:51

+0

這是無關緊要的!如果我直接編寫ragSocialType()而不必使用變量構建名稱,則它可以正常工作。 – Lughino 2013-02-25 11:30:06

回答

1
$class = $field.'Type'; //remove the() 
$form = this->createForm(new $class, $entity); 
+0

返回相同的錯誤! 爲了測試我把$放在變量$ class中: '$ class ='ragSocialeType'; $ form = this-> createForm(new $ class,$ entity); ' 即使如此,它不起作用!始終返回相同的錯誤! 我不明白!在一個普通的類中,它可以工作! – Lughino 2013-02-25 12:14:02

+1

然後在構建類時使用完全限定名稱空間:如$ class =「mybundle \ form \」。 $字段。 '類型'; – user1452962 2013-02-25 12:49:07

+0

謝謝!所以它的作品! – Lughino 2013-02-25 13:20:34