Rendering data from a form to a another view in Symfony 5

  calculator, forms, symfony, symfony5, view

I am making several loan calculators in Symfony 5, all are in my controller SimulatorController.
The user is filling up / submitting a form in the View 1 and should land on View 2 where calculations results appear, along with a recpa of data entered in the form.

So I have a first function in my controller which creates the calculation form, make some calculations, render the form into the view 1 and redirects to View 2 once the form is submitted :


 #[Route('/rachat-credits', name: 'app_rachat')]
    public function rachat(Request $request)
    {
        // Debts rate
        $form = $this->createForm(SimulDebtFormType::class);
        $debtForm = $form->handleRequest($request);

        if($debtForm->isSubmitted() && $debtForm->isValid()){

            $wages = $debtForm->get('salaries')->getdata();
            $bonuses = $debtForm->get('bonuses')->getdata();
            $earnings = $debtForm->get('earnings')->getdata();
            $expenses = $debtForm->get('expenses')->getdata();
            $totalIncome = intval($wages + $bonuses + $earnings);
            $rate = floatval($expenses/$totalIncome*100);
            $debtRate = number_format($rate, 2,',',' ');
            $rest = $totalIncome - $expenses;
            $restToLive = number_format($rest,0,'',' ');

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

        return $this->render('Simul/rachat.html.twig', [
            'debtrateform' => $debtForm->createView(),            
        ]);
    }

And a second function to render the View 2, which displays the results from the previous a form and create a contact form on it.

I don’t manage to pass the retrieved and calculated data into the View 2, I tried to enter parameters in an array into redirectToRoute(), such as:

return $this->redirectToRoute('app_simul_res_endet', [
   'debtRate' => $debtRate,
   'restToLive' => $restToLive,
   'totalIncome' => $totalIncome,
   'expenses' => $expenses,
]);

but the View 2 did not recognized the variables.

<div class="row justify-content-evenly align-items-center text-center fw-bold m-3 endettement">
    <div class="col-sm-6 col-md-4 rounded-3 p-4 mb-4 data">
        <div class="row justify-content-around align-items-center fw-bold">
            <div class="col-10 text-start">
                <p>Mes revenus mensuels</p>
                <p>Mes charges mensuelles</p>
            </div>
            <div class="col-1 text-end figures">
                <p>{{ totalIncome }}€</p>
                <p>{{ expenses }}€</p>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-md-4 results rounded-3 p-4">
        <h3>Estimation de votre 
            taux d'endettement*</h3>
        <h3 class="mensualite">{{ debtRate }}%</h3>
        <h3 class="mt-4">Reste à vivre*</h3>
        <h3 class="mensualite">{{ restToLive }}€</h3>
        <a href="{{ path('app_simul_endet') }}"><button class="btn ctaWhite text-uppercase rounded-pill mt-4">Recalculer</button></a>
    </div>
</div>

Anyone has a clue or advice on how to proceed?

Source: Symfony Questions

LEAVE A COMMENT