私は[symfony2.5]フォームにファイルフィールドを追加します。Symfony2 - 入力フィールドタイプファイル
私は、ファイルを選択するために、ファイルエクスプローラを開き、などの観点で彼のパスを表示したい:私はしたいいけない
:
「//path/to/file.docx C」イメージなどをアップロードします。パス文字列だけです。
私はちょうど私の広告エンティティ内の属性と付け加えた:私のフォーム/ AdvertType.phpで
/**
* @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;
}
は私が追加しました:ここ
->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(メッセージ= "Please、製品のパンフレットをPDFファイルとしてアップロードしてください。")は常にここにあります..
私はいただきました!間違って理解しない私を助けてください..
はいデフォルトのメッセージ「この値は空白にしないでください」が表示されます。 –