Home > database >  Submit Modal Render Controller
Submit Modal Render Controller

Time:12-21

I have a 2 CRUD Controller "Client" and "Equipment". What I want : On the "Client" Show.html.twig i want to add an "Equipement" via a Modal Page.

What already works : Show the form in the modal via {{ render(controller('App\Controller\EquipmentController::new')) }}

The problem : When i Submit the form It doesn't create the equipment without message.

My modal (body only) :

<div >
 {{ render(controller('App\\Controller\\NasController::new')) }}
</div>

My equipment controller (only the new) :


    #[Route('/new', name: 'app_equipment_new', methods: ['GET', 'POST'])]
    public function new(Request $request, NasRepository $nasRepository): Response
    {
        $equipment = new Equipement();
        $form = $this->createForm(EquipementType::class, $equipment);
        $form->handleRequest($request);
    
        if ($form->isSubmitted() && $form->isValid()) {
            $equipmentRepository->save($equipment, true);
    
            return $this->redirectToRoute('app_equipment_index', [], Response::HTTP_SEE_OTHER);
        }
    
        return $this->renderForm('equipment/_new.html.twig', [
            'equipment ' => $equipment ,
            'form' => $form,
        ]);
    }

If needed i can give more code

CodePudding user response:

By default when submitting a form it will send a POST request to the url of the current page. If you want to send the POST request to a different url you have to define it yourself by adding an action to the form

In your case you would have something like this:

$form = $this->createForm(EquipementType::class, $equipment, [
    'action' => $this->generateUrl('app_equipment_new')
]);

Official documentation for this feature is here: https://symfony.com/doc/current/forms.html#changing-the-action-and-http-method

  • Related