0

I have a strange problem on symfony 6.1

I searched, but I couldn't find the solution.

The error message: Neither the property "_token" nor one of the methods "_token()", "get_token()"/"is_token()"/"has_token()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView". The error occurs when validation fails Error screen

    #[Route('/new', name: 'app_commande_new', methods: ['GET', 'POST'])]
    public function new(Request $request, ProductRepository $productRepository): Response
    {
        $commande = new Commande();
        $form = $this->createForm(CommandeType::class, $commande);

        //dd($request);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            dd($form->getData());

            return $this->redirectToRoute('app_commande_index', [], Response::HTTP_SEE_OTHER);
        }
        //dd($form);
        return $this->renderForm('commande/new.html.twig', [
            'commande' => $commande,
            'form' => $form,
        ]);
    }

View

{{ form_start(form) }}
<div class="row">
    <div class="col-md-6">
        {{ form_row(form._token) }}
        {{ form_row(form.client) }}
    </div>
    <div class="col-md-6">
        {{ form_row(form.limitDate) }}
    </div>
</div>

<button class="btn btn-primary" id="save" type="submit">{{ button_label|default('Save') }}</button>
{{ form_end(form, {render_rest: false}) }}

My post can't take more code here are the pictures of the forms code.

jtech237
  • 66
  • 6
  • You need `form_widget` instead of `form_row` for `form._token` – Noah Boegli Jul 20 '22 at 15:01
  • You don't have to add the token field as forms rendered with Symfony add them by default. Remove `{{ form_row(form._token) }}` and look at the html source. Also you probably don't need `{render_rest: false}` in this case.. – Bossman Jul 20 '22 at 15:20
  • @Bossman The problem is that when I open the page to fill out the form, everything works fine. But when I want to modify or check if the validation works correctly, the error occurs. and this despite the fact that I do as you say. – jtech237 Jul 20 '22 at 16:53
  • @jtech237, edit your post with your form class *CommandeType*. "*But when I want to modify or check if the validation works correctly*" - you mean submitting the form? – Bossman Jul 20 '22 at 17:02
  • @jtech237, just realised you have a screenshot (edit your post to correct this). You're trying to render `commande/new.html.twig` where your screenshot says `commande/_form.html.twig`. So remove what i said previously in that template also.. – Bossman Jul 20 '22 at 17:10
  • @Bossman I thank you for helping me. But I don't know why I can't add more code in my post. I edited with screenshots. Everything works perfectly when I go to the ```localhost:8000/command/new``` page If I fill in the form correctly without violating the constraints But when I submit the form violating the constraints I get the error I mentioned – jtech237 Jul 20 '22 at 17:38
  • Did you remove the token and render_rest from `commande/_form.html.twig` aswell? This was where your screenshot error was showing.. – Bossman Jul 20 '22 at 17:46
  • 1
    @Bossman You were right. I mistakenly included the ```command/_form.html.twig``` file in the same file so it was normal that symfony could not find my _token field. Bug solved. Thanks again – jtech237 Jul 20 '22 at 23:50

0 Answers0