Ajax symfony2更新实体脱离模式
几乎整个晚上我都在试图用模态中的一个表单更新一个实体,但它不起作用 我的细枝模板如下所示:Ajax symfony2更新实体脱离模式,ajax,twitter-bootstrap,symfony,modal-dialog,crud,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="tex
{% 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>
错误在于,在单击“编辑”时无法打开模式。您必须添加表单提交:
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(),
);
}
阅读更多关于和
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;
}
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(),
);
}