【发布时间】:2017-10-17 15:01:29
【问题描述】:
我想在我的 [symfony2.5] 表单中添加一个文件字段。
我想打开文件资源管理器选择一个文件,并在视图中显示他的路径:
"C://path/to/file.docx"
我不想上传图片什么的,只是路径字符串。
我刚刚在我的广告实体中添加了该属性:
/**
* @var string
*
* @ORM\Column(type="text", length=255, nullable=false)
* @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
*/
private $attachment;
/**
* Set attachment
*
* @param string $attachment
* @return Advert
*/
public function setAttachment($attachment)
{
$this->title = $attachment;
return $this;
}
/**
* Get attachment
*
* @return string
*/
public function getAttachment()
{
return $this->title;
}
在我的 Form/AdvertType.php 中我添加了:
->add('attachment', 'file')
这是我的 addAction :
public function addAction(Request $request)
{
$advert = new Advert();
$form = $this->createForm(new AdvertType(), $advert);
$usr = $this->get('security.context')->getToken()->getUser();
if ($form->handleRequest($request)->isValid()) {
$em = $this->getDoctrine()->getManager();
$advert->setAuthor($usr->getUsername());
$advert->setDate(new \DateTime);
$em->persist($advert);
$em->flush();
$request->getSession()->getFlashBag()->add('info', 'Annonce bien enregistrée.');
// On redirige vers la page de visualisation de l'annonce nouvellement créée/
return $this->redirect($this->generateUrl('info_view', array('id' => $advert->getId())));
}
return $this->render('SocietyPerfclientBundle:Default:add.html.twig', array(
'form' => $form->createView(),
));
}
我的@Assert\NotBlank(message="请将产品手册上传为 PDF 文件。") 总是在这里..
我不明白怎么了,请帮帮我..
【问题讨论】: