Search code examples
symfonyhttp-redirecturl-parameters

Symfony 5 : Some mandatory parameters are missing ("title") to generate a URL for route "frContentStrategy"


I made a function (createUpdate) in my controller that creates and submits a form which works (database is modified), but the redirection gives me an error Some mandatory parameters are missing ("title") to generate a URL for route "frContentStrategy".

I think I need to pass some arguments to :

return $this->redirectToRoute("frContentStrategy");

Because the redirection "frContentStrategy" uses a parameter converter in its route :

@Route("fr/strategy/content/{title}", name="frContentStrategy")

But until now, I couldn't find a way to make it work.

Here are my files and thank you for your help :

Controller :

/**
 * @Route("fr/strategy/content/{title}", name="frContentStrategy")
 */
public function index(Strategy $strategy): Response
{
    return $this->render('content_strategy/contentStrategy.html.twig', [
        "strategy" => $strategy
    ]);
}

/**
 * @Route("fr/strategy/content/createforce/{title}", name="frCreateForce")
 * @Route("/fr/strategy/content/updateforce/{title}", name="frUpdateForce", methods="GET|POST")
 */
public function createUpdate(DiagnosticForce $diagnosticforce = null, Request $request, EntityManagerInterface $em)
{
    if(!$diagnosticforce){
        $diagnosticforce = new DiagnosticForce();
    }
    $form = $this->createForm(DiagnosticForceType::class, $diagnosticforce);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()){
        $em->persist($diagnosticforce);
        $em->flush();
        return $this->redirectToRoute("frContentStrategy");
    }

    return $this->render('content_strategy/createForce.html.twig', [
        "diagnosticforce" => $diagnosticforce,
        "form" => $form->createView()
    ]);
}

FormType :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('strenght')
        ->add('strategy', EntityType::class,[
            'class' => Strategy::class,
            'choice_label' => 'title'
        ])
    ;
}

contentStrategy.html.twig :

<section class="newStrat">
    <a href="{{path('frStrategy')}}">
        <div>Liste des stratégies</div>
    </a>
</section>
<section>
    <h1>{{strategy.title}}</h1>
    <a href="{{path('frCreateForce', {'title' : strategy.title}) }}">
        <div>Ajouter une force</div>
    </a>
</section>

createForce.html.twig :

{{form_start(form)}}
<div class="divForm1">
    <img class="statue" src="{{asset('img/Calque 2.png')}}" alt="image d'accueil visage statue">
    <h2>Libellé de la force</h2>
    <div class="loginForm">
        {{form_row(form.strenght)}}
    </div>
    <input type="submit" value="Valider">
</div>
{{form_end(form)}}

Solution

  • I finally found the solution. Thank you @craigh for the help and directions.

    To solve the problem I had to change my method createUpdate() to : (everything is in the controller file)

    public function createUpdate(Strategy $id, DiagnosticForce $diagnosticforce = null, Request $request, EntityManagerInterface $em)
    

    Then I added a parameter to my redirection :

    return $this->redirectToRoute("frContentStrategy", [
                'title' => $id->getTitle()
            ]);