Symfony 2 | Form exception when modifying an object that has a file(picture) field Symfony 2 | Form exception when modifying an object that has a file(picture) field symfony symfony

Symfony 2 | Form exception when modifying an object that has a file(picture) field


I solved the problem setting data_class to null as follows:

public function buildForm(FormBuilderInterface $builder, array $options){    $builder    ->add('title')    ->add('picture', 'file', array('data_class' => null)    );}


I would recommend you to read the documentation of file upload with Symfony and Doctrine How to handle File Uploads with Doctrine and a strong recommendation to the part Lifecycle callbacks

In a brief you usually in the form use the 'file' variable (see documentation), you can put a different label through the options, then in your 'picture' field, you just store the name of the file, because when you need the src file you can just call getWebpath() method.

->add('file', 'file', array('label' => 'Post Picture' ));

to call in your twig template

<img src="{{ asset(entity.webPath) }}" />


Please make below change in your PostType.php.

public function buildForm(FormBuilderInterface $builder, array $options){    $builder    ->add('title')    ->add('picture', 'file', array(            'data_class' => 'Symfony\Component\HttpFoundation\File\File',            'property_path' => 'picture'        )    );}