Reputation: 587
almost whole night I'm trying to update an entity with a form in a modal, but it doesn't work...
My twig template looks like :
{% 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>
the modal template looks like:
<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">×</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>
I think I got problems with the variable in the URl but I don't know how to fix it.
This is the part of my controller:
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;
}
The Error is that the modal doesn't open at click on edit
Upvotes: 0
Views: 1335
Reputation: 720
You have to add form submission:
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(),
);
}
Read more about forms and symfony 2.7 demo
Upvotes: 2