2013-02-25 169 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