Symfony编辑表单不工作
问题描述:
我想用symfony表格编辑我的数据,并且我的问题 可能与我的控制器有关。我有这样的一些: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'),
));
}
所有显示corectly,但当我点击一个按钮我的网页重新加载(不保存更改)。感谢帮助。
答
这是您的控制器方法的问题。下面应该适合你。
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);
实体管理器不知道如何保存表单数据,它仅用于实体。尝试移交控制器中的'$ order'实体,在有效条件块内应更新新的有效表单数据。 – JimL
这是不工作:( – MrMajk