0

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)}}
William
  • 5
  • 3
  • You are right, you need to add a title parameter. the second answer here can help. https://stackoverflow.com/a/41922683/2600812 – craigh Dec 23 '20 at 12:29
  • Thank you but now I have an error App\Entity\Strategy object not found by the @ParamConverter annotation., I added (Strategy $title) as arguments in my function createUpdate(), and change my redirection to : return $this->redirectToRoute("frContentStrategy", [ 'title' => $title ]); – William Dec 23 '20 at 12:53
  • that's because you have improperly set up your action method arguments. The `$title` will be used in the `ParamConverter` to automatically look up the associated `Strategy`. It assumes the value of `$title` is a `Strategy` id. You should research how to properly set up the method arguments to get the information you require in the method. https://symfony.com/doc/current/controller.html#a-basic-controller and https://symfony.com/doc/current/controller/argument_value_resolver.html – craigh Dec 23 '20 at 12:59

1 Answers1

0

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()
        ]);
William
  • 5
  • 3