symfony2更新实体的模态

时间:2015-04-26 00:37:33

标签: ajax twitter-bootstrap symfony modal-dialog crud

几乎整夜我试图用模态更新一个实体,但它不起作用......

我的树枝模板如下:

    {% 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 个答案:

答案 0 :(得分: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(),
        );
}

详细了解formssymfony 2.7 demo