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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?