1
votes

i wanna add a file field in my [symfony2.5] form.

I want to open the file explorer to select a file , and display his path in the view like :

"C://path/to/file.docx"

I dont want to upload a image or whatever, just the path string.

i just added that attribute in my Advert Entity :

    /**
     * @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;
    }

in my Form/AdvertType.php i've added :

->add('attachment', 'file')

Here is my 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="Please, upload the product brochure as a PDF file.") is always here.. enter image description here

Got this error : enter image description here

I dont understand whats wrong please help me..

1

1 Answers

0
votes

Don't you need a $form->isSubmitted() with your verification $form->isValid() ? If you let @Assert\NotBlank() without message is there the "This value should not be blank" default message appearing ?