V-labs / VlabsMediaBundle

Form, database and filesystem abstraction for files
44 stars 18 forks source link

File object instead of String #14

Closed Spomky closed 11 years ago

Spomky commented 11 years ago

Hi,

I have just installed this bundle. I have created an Image entity and linked it to another entity. Then I have created my form and successfully uploaded an image. But when I want to edit my entity, I have the following error : 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.

It seems that your data transformer return a string, but Symfony does not accept it and prefer a File object. Do you know how to resolve this error?

choomz commented 11 years ago

Hi,

Is your mapping correct ? It looks like you are not using the right form type, or you don't extends from VlabsFile.

Spomky commented 11 years ago

My entity extends VlabsFile and I use vlabs_file in my form type. My entity:

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Vlabs\MediaBundle\Entity\BaseFile as VlabsFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class Image extends VlabsFile
{
    /**
     * @ORM\Column(name="path", type="string", length=255)
     * @Assert\Image()
     */
    private $path;

    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    public function getPath()
    {
        return $this->path;
    }
}

and my form type:

<?php

namespace Acme\DemoBundle\Form\Card;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CardType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                …
            ))
            ->add('image', 'vlabs_file', array(
                'required' => true,
                "label"=>"Image",
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\Card'
        ));
    }

    public function getName()
    {
        return 'acme_demo_cardtype';
    }
}
Spomky commented 11 years ago

The image is uploaded and is in the folder I've selected. Nothing wrong exect when I want to edit my entity.

choomz commented 11 years ago

Ok, this looks good. Is everythink OK after first image creation ?

I mean :

It crash when you try to display the edit form ? Or when you are trying to update your image ?

Actually the error looks like you are not dealing with a BaseFileInterface. The aim of the bundle is to take control over File object, with the BaseFileInterface exposed.

Everything is handled by the BaseFileListener, during the form process. Check your form types, i guess it is linked to mapping.

Actually, this part is the base part of the bundle, and it should work.

choomz commented 11 years ago

Looks like we answered at the same time. Can you try to check my previous post ? What is your Symfony version ?

Spomky commented 11 years ago

In fact it is quite stupid to edit like this. I have created a type to import my images and made a media manager to link images with my cards. So cards and images are managed independently.

It works fine now.