N
N
NubasLol2018-07-17 22:02:15
symfony
NubasLol, 2018-07-17 22:02:15

Symfony forms how to make file upload optional?

I pass the object retrieved by id to the form, initially each object receives an image when it is created. But when editing, it again requires you to upload a picture to it

->add('img',FileType::class, array(
                'required' => false,
  
            ))

The img field in the doctrine entity stores the string
/**
     * @ORM\Column(type="string", nullable=true)
     * @Assert\File(mimeTypes={ "image/jpeg" })
     */
    private $img;

I tried to set it in the default doctrine, and I constantly catch errors
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Martin Alekseevich, 2018-07-18
@martin_alekseevich

The documentation is all about how to upload files
When creating a form to edit an already persisted item, the file form type still expects a File instance. As the persisted entity now contains only the relative file path, you first have to concatenate the configured upload path with the stored filename and create a new File class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question