【问题标题】:Symfony edit form dosen't workSymfony 编辑表单不起作用
【发布时间】:2017-12-20 17:02:52
【问题描述】:

我想用 symfony 表单编辑我的数据,但我遇到了问题 可能与我的控制器。我有一些这样的:

  public function detailAction($id,Request $request)
{
    $order = $this->getDoctrine()->getRepository(OrderMain::class)->find($id);

    if (!$order) {
        throw $this->notFoundException();
    }

    $form = $this->createForm(OrderMainType::class, $order);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
//            do not enter here
            $orderEdit = $form-getData();
            $em = $this->getDoctrine()->getManager();
            $em->persist($orderEdit);
            $em->flush();
        }

    return $this->render('ModiModiAdminBundle:Order:detail.html.twig',array(
        'form' => $form->createView(),
        ));
}


 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
   /.../
 ->add('edit', SubmitType::class, array(
                                  'attr' =>array('class'=>'edit'),
            ));
    }

全部显示正确,但是当我单击按钮时,我的页面会重新加载(不保存更改)。感谢帮助。

【问题讨论】:

  • 实体管理器不知道如何持久化表单数据,它只接收实体。尝试在您的控制器中移交 $order 实体,该实体在 isvalid 条件块内应使用新的有效表单数据进行更新。
  • 这行不通:(

标签: php forms symfony submit edit


【解决方案1】:

这是您的控制器方法的问题。下面应该适合你。

public function detailAction($id,Request $request)
{
    $order = $this->getDoctrine()->getRepository(OrderMain::class)->find($id);

    if (!$order) {
        throw $this->notFoundException();
    }

    $form = $this->createForm(OrderMainType::class, $order);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
            //do not enter here
            $em = $this->getDoctrine()->getManager();
            $em->flush();
        }

    return $this->render('ModiModiAdminBundle:Order:detail.html.twig',array(
        'form' => $form->createView(),
        ));
}

您可以删除$orderEdit = $form-getData(); 的行。提交表单时,应根据提交的数据更新实体。由于这已经是一个托管实体,您还可以删除$em->persist($orderEdit);

【讨论】:

  • 也许可以,但不要进入 if ($form->isSubmitted() && $form->isValid())
  • 表单提交有效后“不输入”是什么意思?
  • 是的,$form->isSubmitted() 和 $form->isValid() 总是返回 false
  • 那么问题很可能在您的表单中。如果您使用的是配置文件调试器,您应该能够看到正在发送的数据以及响应中的任何错误。
【解决方案2】:
public function edit(Request $request)
{
 $id = $request->get('id');
 $category = $this->getDoctrine()->getRepository(Category::class)->find($id);

    if (!$category) {
        throw $this->notFoundException();
    }
    $form = $this->createForm(CategoryType::class,$category);
    $form->handleRequest($request);
    if($form->isSubmitted() && $form->isValid()){
        $em = $this->getDoctrine()->getManager();
        $categoryData = $form->getData();
        $em->persist($categoryData);
        $em->flush();
    }

    return $this->render('admin/category/edit.html.twig',array(
        'form' => $form->createView(),
    ));
}

你错过了 => 处理来自代码的请求 $form->handleRequest($request);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2017-02-27
    • 1970-01-01
    相关资源
    最近更新 更多