2015-04-26 32 views
0

几乎整个晚上我试图更新的模式形式的实体,但它不工作的...Symfony2的更新实体进行模态

我的树枝模板的样子:

{% for entity in entities %} 
    <tr> 
    <td class="text-center">{{ entity.id }}</td> 
    <td class="text-center">{{ entity.cashbackDays }} Tage</td> 
    <td class="text-center">{{ entity.cashbackPercent }} %</td> 
    <td class="text-center">{{ entity.nettoDays }} Tage</td> 
    <td class="text-center"> 
     <a data-toggle="modal" data-target="#editCashbackModal" class="btn btn-xs btn-default" href="{{ path('cashback_edit', { 'id': entity.id }) }}"><i 
      class="fa fa-edit"></i></a> 
    </td> 
    </tr> 
{% endfor %} 
</tbody> 
     <div class="modal fade" id="editCashBackModal" tabindex="-1" role="dialog" aria-labelledby="editCashBackModalLabel" aria-hidden="true"> 

     </div> 

模态模板的样子:

<div class="modal-dialog"> 
    <div class="modal-content"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span 
      aria-hidden="true">&times;</span></button> 
     <h4 class="modal-title" id="editCashBackModalLabel">Skontoschlüssel bearbeiten</h4> 
    </div> 
    <div class="modal-body"> 
     {{ form(edit_form) }} 

     <ul class="record_actions"> 
     <li> 
      <a href="{{ path('cashback') }}"> 
      Back to the list 
      </a> 
     </li> 
     <li>{{ form(delete_form) }}</li> 
     </ul> 
    </div> 
    <div class="modal-footer"> 
    </div> 
    </div> 
</div> 

我想我在URL中的变量问题,但我不知道如何解决它。

这是我的控制器的一部分:

public function editAction($id) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $entity = $em->getRepository('sulzerAppBundle:Cashback')->find($id); 

    if (!$entity) { 
     throw $this->createNotFoundException('Unable to find Cashback entity.'); 
    } 

    $editForm = $this->createEditForm($entity); 
    $deleteForm = $this->createDeleteForm($id); 

    return array(
     'entity'  => $entity, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    ); 
} 

/** 
* Creates a form to edit a Cashback entity. 
* 
* @param Cashback $entity The entity 
* 
* @return \Symfony\Component\Form\Form The form 
*/ 
private function createEditForm(Cashback $entity) 
{ 
    $form = $this->createForm(new CashbackType(), $entity, array(
     'action' => $this->generateUrl('cashback_update', array('id' => $entity->getId())), 
     'method' => 'PUT', 
    )); 

    $form->add('submit', 'submit', array('label' => 'Update')); 

    return $form; 
} 

的错误是,模式不以点击编辑

+1

什么是错误? – ihsan

回答

2

打开你要添加的表单提交:

protected $requestStack; 

    public function __construct(RequestStack $requestStack) 
    { 
     $this->requestStack = $requestStack; 
    } 

    public function editAction($id) 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $entity = $em->getRepository('sulzerAppBundle:Cashback')->find($id); 

     if (!$entity) { 
     throw $this->createNotFoundException('Unable to find Cashback entity.'); 


     $editForm = $this->createEditForm($entity); 
     $deleteForm = $this->createDeleteForm($id); 

     $editForm->handleRequest($this->requestStack);  

     if ($editForm->isSubmitted() && $editForm->isValid()) { 
      /** @var Cashback $cashback */ 
      $cashback = $editForm->getData(); 
      ... 
      $em->persist($cashback); 
      $em->flush(); 
     } 
     ... 
     return array(
      'entity'  => $entity, 
      'edit_form' => $editForm->createView(), 
      'delete_form' => $deleteForm->createView(), 
     ); 
} 

查看更多about formssymfony 2.7 demo