【发布时间】:2015-11-17 08:22:04
【问题描述】:
我有一个控制器,我在其中创建一个打开的作业列表并通过 Twig 将它们填充到一个表中。 我现在想要的是每行的最后一个字段是一个上传表单,以便您可以将文件添加到一个特定的作业中。不幸的是,我不知道如何在一个控制器中处理多个表单的表单请求。
这是我现在拥有的控制器:
/**
* @Route("/job/pending", name="pendingJobs")
*/
public function jobAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$file = new File();
$form = $this->createFormBuilder($file)
->add('file')
->add('job','entity',array(
'class' => 'AppBundle:Job',
'choice_label' => 'insuranceDamageNo',
))
->add('save', 'submit', array('label' => 'Create Task'))
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$job = $em->getRepository("AppBundle:Job")->find($form->getData()->getJob());
$file->setFile($form->getData()->getFile());
$file->setPath($form->getData()->getPath());
$file->setJob($job);
$em->persist($file);
$em->flush();
return $this->redirectToRoute("pendingJobs");
}
$jobs = $em->getRepository("AppBundle:Job")->findBy(array(
'receipt' => true,
'receiptStatus' => true,
));
return $this->render(
'default/pending.html.twig',
array(
'jobs' => $jobs,
'form' => $form->createView(),
)
);
}
除了它只是一个表单并且“工作”实体是一个下拉列表之外,该表单可以完美运行。如果可能的话,我希望为每个工作“预先选择”它以获得正确的 ID。
我发现了一些关于“createNamedBuilder”HERE(最后一篇文章)的内容,但它是法语的,我不懂法语,API 也没有帮助。
我想过为$jobs 设置一个foreach,但是如何分隔表单句柄?
感谢任何提示!
【问题讨论】:
-
请显示您的文件实体
-
pastebin.com/yNVxpwjL 你去...
标签: php forms symfony formbuilder