2017-10-17 96 views
1

我想在我的[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文件。」)總是在這裏。 enter image description here

得到這個錯誤:enter image description here

我不明白什麼是錯,請幫助我..

回答

0

不要你需要一個$形式 - > isSubmitted()與您的驗證$形式 - >的isValid ()? 如果讓@Assert \ NotBlank()沒有消息,那麼顯示的「此值不應該是空白的」默認消息?

+0

是的默認消息「此值不應該爲空」出現.. –

相關問題