Data don’t persist from modal – Symfony 4

Hello everybody !
I’m a beginner at dev and Symfony.

I want to display a form in a Bootstrap modal, which works perfectly, but it doesn’t persist in database when I submit it. (This form worked perfectly, persist and eveything, before I put it on a modal)

I found some ressources to help, but it’s never the same problem than mine

If anyone could help me, it would be so perfect !
Thank you =)

This is my controller :

public function new(Request $request): Response
    {
        $favorite = new Favorite();
        $newFavoriteForm = $this->createForm(NewFavoriteType::class, $favorite);
        $newFavoriteForm->handleRequest($request);

        if ($newFavoriteForm->isSubmitted() && $newFavoriteForm->isValid()) {
            dd($newFavoriteForm);
            $favorite->setCustomer($this->getUser()->getCustomer());
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($favorite);
            $entityManager->flush();

            return $this->redirectToRoute('/');
        }

        return $this->render('/tcheen/profile_customer/new_favorite.html.twig', [
            'favorite' => $favorite,
            'new_favorite_form' => $newFavoriteForm->createView(),
        ]);
    }

This is my Twig :

<div class="modal fade" id="new-favorite-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Nouveau favori</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <h2> Créer un nouveau favori</h2>
               {{ form_start(new_favorite_form) }}
                {{ form_widget(new_favorite_form) }}
                <button class="btn btn-success">{{ button_label|default('Ajouter') }}</button>
                {{ form_end(new_favorite_form) }}
            </div>

        </div>
    </div>
</div>

And the view where it is display :

 <div>
                Pas encore de favoris ?
                <a href="{{ path('favorite_new') }}" data-toggle="modal" data-target="#new-favorite-modal">
                    <button type="button" class="btn main-button">
                        Créer un favori
                    </button>
                </a>
                {{ render(controller('AppControllerFavoriteController::new')) }}
                {#{{ include ('tcheen/profile_customer/new_favorite.html.twig') }}#}

            </div>

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *