sarah
sarah

Reputation: 105

Request object is null - Symfony 3.4

I'm having a problem with a submitted form.

I create a form object in a controller method, and I specify that when submitted, it should be handled in another action method.

    $invoiceForm = $oldInvoiceForm ?? $this->createForm(InvoiceProjectInvoiceType::class, $invoiceProject, [
        'action' => $this->generateUrl('invoice_edit', ['id' => $invoiceProject->getId()]),
    ]);

Here's the invoice_edit route

/**
 * @param Request $request
 * @param InvoiceProject $invoiceProject
 * @return Response
 * @Route("/{id}/edit/invoice", name="invoice_edit", methods="POST")
 */
public function editInvoice(Request $request, InvoiceProject $invoiceProject){

    $form = $this->createForm(InvoiceProjectInvoiceType::class, $invoiceProject);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $this->getDoctrine()->getManager()->flush();
        $this->addFlash("notice", "La config a bien été créée");
        return $this->redirectToRoute('invoice_show', ['id' => $invoiceProject->getId()]);
    }
    else{
        return $this->forward("ClientBundle:InvoiceProject:show", [
            'id' => $invoiceProject->getId(),
            'oldInvoiceForm' => $form
        ]);
    }
}

I do get to that method but I get this error message:

Controller "ClientBundle\Controller\InvoiceProjectController::editInvoice()" requires that you provide a value for the "$request" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

So I do a little test, I put "Request $request = null", to make it optional, and it goes through. Obviously it crashes later in the handleRequest part, but I wonder why the request Object isn't injected in the action... Here's the html of rendered form (without the inputs because there are lots of them)

    <form name="clientbundle_invoiceproject_invoice" method="post" action="/client/131/edit/invoice">                
    <button type="submit" class="btn btn-secondary">Modifier les informations</button>
     </form>

Does anyone see anything unusual in the code ?

Upvotes: 1

Views: 1884

Answers (1)

BENARD Patrick
BENARD Patrick

Reputation: 30975

It seems that you don't have include the Request class in your file header

<?php

namespace App\AppBundle\Controller;


use Symfony\Component\HttpFoundation\Request;

Upvotes: 2

Related Questions