devswaam
devswaam

Reputation: 97

Catchable Fatal Error: Argument 1 passed to \Entity\Image::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile,

I Got this error

Catchable Fatal Error: Argument 1 passed to swaam\ImageUploaderBundle\Entity\Image::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile,

instance of swaam\ImageUploaderBundle\Entity\Image given, called in D:\xamp\htdocs\shams\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 438 and defined in D:\xamp\htdocs\shams\src\swaam\ImageUploaderBundle\Entity\Image.php line 166

according to the mention line no 166 its the function setFile() here is my function

 public function setFile(UploadedFile $file = null)
{
    $this->file = $file;
    // check if we have an old image path
    if (isset($this->path)) {
        // store the old name to delete after the update
        $this->temp = $this->path;
        $this->path = null;
    } else {
        $this->path = 'initial';
    }
}

I couldn't even understand the error , any one please help me and explain a bit the reason of the error plus the possible solution.

Upvotes: 1

Views: 2729

Answers (2)

Konstantin Pereiaslov
Konstantin Pereiaslov

Reputation: 1814

I got this error when using choice field type in a form for a field that represented an entity.

To fix this I changed choice to entity in buildForm and also added class that this entity is supposed to resolve to to options, so add statement started to look as following:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add(
                  'product', 
                  'entity', 
                  [
                      'class' => 'Acme\AppBundle\Entity\Product',
                  ]
    );
 }

Upvotes: 1

Xover
Xover

Reputation: 598

I've got a similar error message. I solved it by adding <form method="post" {{ form_enctype(form) }}> to my form tag.

Upvotes: 4

Related Questions