我有一个控制器,我在其中创建一个打开的作业列表,并通过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(),
)
);
}
除了它只是一个表单并且"Job"实体是一个下拉列表之外,该表单工作得很好。如果可能的话,我希望每个工作都"预先选择"正确的id。
我在这里找到了一些关于"createNamedBuilder"的内容(上一篇文章),但它是法语的,我不懂法语,API也没有任何帮助。
我想为$jobs
创建一个foreach,但如何分离表单句柄?
感谢任何提示!
在控制器中创建三个操作。一个用于主页,一个用于每个上传表单,一个处理表单:
/**
* @Route("/job", name="pendingJobs")
*/
public function jobAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$jobs = $em->getRepository("AppBundle:Job")->findAll();
return $this->render(
'default/pending.html.twig', array(
'jobs' => $jobs,
)
);
}
/*
* renders an uploadform as a partial from jobAction
*/
public function jobrowAction(Request $request, Job $job) {
//$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$file = new File();
$file->setJob($job); // so that we know to what job this upload belongs!
$form = $this->createUploadForm($file, $job->getId());
return $this->render(
'default/pending_job_row.html.twig', array(
'job' => $job,
'form' => $form->createView(),
)
);
}
/*
* renders and processes an uploadform
*
* @Route("/job/{id}/update", name="job_upload")
* @Method("POST")
*/
public function uploadAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$file = new File();
// this time we set the job property again cause we only receiced the jobId from the route
$job = $em->getRepository("AppBundle:Job")->findOneBy(array('id' => $id));
if (!$job) {
throw $this->createNotFoundException('Unable to find Job entity.');
}
$file->setJob($job);
$form = $this->createUploadForm($file, $id);
$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");
}
// if the form is not valid show the form again with errors
return $this->render(
'default/error.html.twig', array(
'form' => $form->createView(),
)
);
}
private function createUploadForm(File $file, $jobId)
{
$form = $this->createFormBuilder($file, array(
'action' => $this->generateUrl('job_upload', array('id' => $jobId)),
'method' => 'POST',
))
->add('file')
->add('save', 'submit', array('label' => 'Create Task'))
->getForm();
return $form;
}
然后制作两个Twig文件:
{# default/pending.html.twig #}
{% extends 'base.html.twig' %}
{% block body %}
<table>
{% for job in jobs %}
<tr>
<td>{{ job.title }}</td>
<td>{{ render(controller('AppBundle:Default:jobrow', { 'job': job })) }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
和:
{# default/pending_job_row.html.twig #}
{{ form(form) }}
在文件实体中缺少两种方法:
/**
* Set job
*
* @param 'AppBundle'Entity'Job $job
*
* @return File
*/
public function setJob('AppBundle'Entity'Job $job = null)
{
$this->job = $job;
return $this;
}
/**
* Get job
*
* @return 'AppBundle'Entity'Job
*/
public function getJob()
{
return $this->job;
}
我将用法语post逻辑和你的回答:
/**
* @Route("/job/pending", name="pendingJobs")
*/
public function jobAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$jobs = $em->getRepository("AppBundle:Job")->findBy(array(
'receipt' => true,
'receiptStatus' => true,
));
foreach($jobs as $job) {
$file = new File();
$form = $this->get('form.factory')
->createNameBuilder($job->getId(), new FileType(), $job)
->getForm();
$form->handleRequest($request);
$forms[] = $form->createView();
if ($form->isValid()) {
$job = $em->getRepository("AppBundle:Job")->find($form->getName());
$file->setFile($form->getData()->getFile());
$file->setPath($form->getData()->getPath());
$file->setJob($job);
$em->persist($file);
$em->flush();
return $this->redirectToRoute("pendingJobs");
}
}
return $this->render(
'default/pending.html.twig',
array(
'jobs' => $jobs,
'forms' => $forms,
)
);
}
为了更干净,创建一个单独的表单类型:
<?php
namespace AppBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolverInterface;
class FileType extends AbstractType
{
public function getName()
{
return 'my_file_type';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file')
->add('job','entity',array(
'class' => 'AppBundle:Job',
'choice_label' => 'insuranceDamageNo',
))
->add('save', 'submit', array('label' => 'Create Task'))
}
}