【问题标题】:Symfony2 - Input field type fileSymfony2 - 输入字段类型文件
【发布时间】: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 文件。") 总是在这里..

收到此错误:

我不明白怎么了,请帮帮我..

【问题讨论】:

    标签: file symfony input


    【解决方案1】:

    您不需要 $form->isSubmitted() 验证 $form->isValid() 吗? 如果您让@Assert\NotBlank() 没有消息,是否会出现“此值不应为空白”的默认消息?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-17
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2015-02-10
      相关资源
      最近更新 更多