Symfony2 - 输入字段类型文件
问题描述:
我想在我的[symfony2.5]表单中添加一个文件字段。Symfony2 - 输入字段类型文件
我想打开文件浏览器选择一个文件,并在如视图中显示了他的去路:
“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;
}
在我的表格/ 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(),
));
}
My @Assert \ NotBlank(message =“请将产品手册上传为PDF文件。”)总是在这里。
我不明白什么是错,请帮助我..
答
不要你需要一个$形式 - > isSubmitted()与您的验证$形式 - >的isValid ()? 如果让@Assert \ NotBlank()没有消息,那么显示的“此值不应该是空白的”默认消息?
是的默认消息“此值不应该为空”出现.. –