Reputation: 198
I have a list of cars, each car has a button, and pressing the button allows me to access a modal form to write a ticket for that specific car. Example: https://i.sstatic.net/tUnf0.jpg
The problem is that I'm having some problems integrating the modal and the form inside my twig.
The form I'm supposed to fill has 4 inputs alltogether. the first 2 are supposed to get pre-filled by data passed from the car related to the pressed button, the rest I can fill myself. But I don't know how to pass these informations from the twig to the form.
What I did so far :
Problem : Pressing the button won't show anything.
My code :
index.html.twig
{% block title %}Parking index{% endblock %}
{% block body %}
<table id="file_export" class="table table-striped table-bordered">
<tbody>
{% for voitures in voiture %}
<tr>
<td>
{{ voitures.matricule }}
</td>
<td>
{{ voitures.parking.libelle }}
</td>
<td>
<span class="timer" data-expires="{{ voitures.getExpiresAt() }}"></span>
</td>
<td>
<button type="button" class="btn btn-dark" href="{{ path('new_ticket', {'id': voitures.id},{'parking': voitures.parking.id}) }}" data-toggle="modal" data-target="#createmodel" data-whatever="{{ voitures.id }}">ticket</button>
</td>
</tr>
{% endif %}
{% endfor %}
My modal.html.twig form:
<div class="modal fade" id="createmodel" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form>
<div class="modal-header">
<h5 class="modal-title" id="createModalLabel"><i class="ti-marker-alt m-r-10"></i> ticket for:</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form_start(form) }}
{{ form_row(form.Matricule, { 'label': 'Matricule' }) }}
{{ form_row(form.Parking, { 'label': 'Matricule' }) }}
{{ form_row(form.Date, { 'label': 'Date' }) }}
{{ form_row(form.montant, { 'label': 'Montant' }) }}
<button class="btn">{{ button_label|default('Add') }}</button>
{{ form_end(form) }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success"><i class="ti-save"></i> Save</button>
</div>
</form>
</div>
</div>
and finally my controller (which doesn't work)
/**
* @Route("/ticket", name="new_ticket", methods={"GET","POST"})
*/
public function newTicket(Request $request): Response
{
$ticket = new Ticket();
$form = $this->createForm(TicketType::class, $ticket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->addFlash('success','ammende added !');
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($ticket);
$entityManager->flush();
return $this->redirectToRoute('agent');
}
return $this->render('Agent/modal.html.twig', [
'ticket' => $ticket,
'form' => $form->createView(),
]);
}
This is the solution I found : I used render to integrate the form inside my index, it's not perfect but it works:
{{render(controller('App\\Controller\\AgentController:newTask')) }}
Upvotes: 1
Views: 13761
Reputation: 46
There are several errors in your code.
The first error is that you have declared two forms one inside the other.
{{ form_start(form) }}
prints <form>
tag. The second problem is that you have not associated a form to a car. For example you should render a modal for every car and pass it the route where do you want post.
I leave here an example
{% for voitures in voiture %}
<tr>
<td>
{{ voitures.matricule }}
</td>
<td>
{{ voitures.parking.libelle }}
</td>
<td>
<span class="timer" data-expires="{{ voitures.getExpiresAt() }}"></span>
</td>
<td>
<button type="button" class="btn btn-dark" href="" data-toggle="modal" data-target="#createmodel" data-whatever="{{ voitures.id }}">ticket</button>
</td>
</tr>
{% set submit_url = path('new_ticket', {'id': voitures.id},{'parking': voitures.parking.id}) %}
{% include 'modal_form.html.twig' wiht {url: submit_url}
{% endfor %}
And the modal it can be:
<div class="modal fade" id="createmodel" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
{{ form_start(form, {'action': url)}) }}
<div class="modal-header">
<h5 class="modal-title" id="createModalLabel"><i class="ti-marker-alt m-r-10"></i> ticket for:</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form_row(form.Matricule, { 'label': 'Matricule' }) }}
{{ form_row(form.Parking, { 'label': 'Matricule' }) }}
{{ form_row(form.Date, { 'label': 'Date' }) }}
{{ form_row(form.montant, { 'label': 'Montant' }) }}
<div class="modal-footer">
</div>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success"><i class="ti-save"></i> Save</button>
</div>
{{ form_end(form) }}
</div>
</div>
</div>
Upvotes: 3