azzaze
azzaze

Reputation: 151

Symfony 4 - Impossible to submit my form modal (error with path)

in my project symfony 4 I have a button that I use to open a modal popup, which has a form, like this:

enter image description here

But the problem is that when I submit the form, I have this error:

No route found for "POST /validation/%7B%7BdeleteLink%7D%7D" (from "http://127.0.0.1:8000/validation/absences")

In my Controller, I've my index function which pass my form :

/**
 * @Route("/validation/absences", name="validation_index")
 */
public function index(PaginatorInterface $paginator, Request $request, AbsenceService $absenceService)
{
     $refusAbsence = new Absence();
     $formRefus = $this->createForm(RefusAbsenceType::class, $refusAbsence);
     $formRefus->handleRequest($request);
     //.... render, etc
}

And in my Twig, I display my form like this :

{% import 'macro/macro.html.twig' as macro %}
{% block body %}
//...
<a class="btn btn-secondary ml-1" href="#" data-target="#refuserAbsenceModal{{demande.id}}" data-toggle="modal">
   <i class="fas fa-times"></i>
</a>
{{ macro.create_form_modal(
      'refuserAbsenceModal'~demande.id,
       "Refuser l'absence ?",
       formRefus,
       path('validation_refuser',{'id': demande.id})
       )
}}

And in my macro.html.twig :

{%- macro create_form_modal(id, title, form, deleteLink) -%}
    {% filter spaceless %}
        <div id="{{ id }}" class="modal fade" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">
                    {{form_start(form, {'action': "{{deleteLink}}"})}}
                    <div class="modal-header">
                        <h4 class="modal-title">{{title}}</h4>
                        <button class="close" data-dismiss="modal" type="button">&times;</button>
                    </div>
                    <div class="modal-body">
                        {{form_widget(form)}}
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Valider</button>
                        <button class="btn btn-secondary" data-dismiss="modal" type="button">Annuler</button>
                    </div>
                    {{form_end(form)}}
                </div>
            </div>
        </div>

    {% endfilter %}

{%- endmacro -%}

It has to call my controller function :

/**
 * Refuser une demande d'absence
 * 
 * @Route("validation/absences/refuser/{id}", name="validation_refuser")
 * @Method({"POST"})
 *
 * @param Absence $absence
 * @return void
 */
public function refuser(Absence $absence)
{
    dd($absence);
}

Is the problem coming from the fact that I declare my form in a function, and that when I submit it, I ask it to point to another function? Will I have to do everything in the same function?

Upvotes: 0

Views: 411

Answers (1)

u_mulder
u_mulder

Reputation: 54841

Error is here:

{{form_start(form, {'action': deleteLink})}}

deleteLink is a variable and there's no need to enclose it with quotes.

And I suppose we should close this as offtopic(.

Upvotes: 1

Related Questions