douyan7916 2018-07-20 12:19
浏览 21
已采纳

请求对象为空 - 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 ?

  • 写回答

1条回答 默认 最新

  • douwu8251 2018-07-20 12:25
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页