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 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题