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://imgur.com/a/t82H9UT
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 :
- I have my list, my button in an index.html.twig file, and I have my form inside a model in a separate twig called modal.html.twig, I also made a controller.
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')) }}