3
votes

This is my error:

The form's view data is expected to be an instance of class My\Bundle\Entity\Tags, but is an instance of class Doctrine\Common\Collections\ArrayCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\Common\Collections\ArrayCollection to an instance of My\Bundle\Entity\Tags

and this is my form builder

 $builder
            ->add('name')
            ->add('tags','collection',array(
                        'data_class' => 'My\Bundle\Entity\Tags'
                        )
            )
            ->add('save','submit')
        ;

I changed data_class to null ( only that ) and I'm getting error:

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, **but is an instance of class My\Bundle\Entity\Tags*. You can avoid this error by setting the "data_class" option to "My\Bundle\Entity\Tags" or by adding a view transformer that transforms an instance of class My\Bundle\Entity\Tags to scalar, array or an instance of \ArrayAccess.

I've tried with a transformer, so it looked like this :

    $transformer = new TagTransformer($this->entityManager);
    $builder
        ->add(
            $builder->create(
                'tags','collection',array(
                    'data_class' => 'My\Bundle\Entity\Tags'
                    )
            )->addModelTransformer($transformer)
        );

and transformer:

public function transform($tag)
{
    if (null === $tag) {
        return "";
    }

    return $tag->toArray();
}

and changed data_class to null again. What I get:

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class My\Bundle\Entity\Tags. You can avoid this error by setting the "data_class" option to "My\Bundle\Entity\Tags" or by adding a view transformer that transforms an instance of class My\Bundle\Entity\Tags to scalar, array or an instance of \ArrayAccess.

When I changed data_class to My\Bundle\Entity\Tags

The form's view data is expected to be an instance of class My\Bundle\Entity\Tags, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of My\Bundle\Entity\Tags.

Well.. I mean... wtf? What am I doing wrong? How can I change that?

Edit:

My user entity:

class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="Tags", cascade={"persist"})
     */
    protected $tags;

// methods, etc..
}
2
post your tags declaration in your entity - Derick F
Pretty sure you will need to make yourself a TagFormType. I don't see how S2 will be able to make a form for you Tag(s) entity without one. Follow the example in the cookbook: symfony.com/doc/current/cookbook/form/form_collections.html - Cerad
And rename Entity\Tags to Tag otherwise it will drive you crazy. - Cerad
well I can't follow the tutorial because I need to: render collection as multiple choice list AND as a multiple text inputs. And also I do have a TagFormType, but it has only one field, which is "title". @DerickF I've edited my post, is this is what You meant? - mmmm

2 Answers

4
votes

So the reason you're getting errors is because you're using the collection field type a bit incorrect. First of all, the collection field type doesn't support data_class. When you say

->add('tags','collection',array(
    'data_class' => 'My\Bundle\Entity\Tags'
     )
)

you're basically saying that tags (which is an array collection according to your declaration) is actually a tag. If you look at the documentation for the collection type you'll notice that data_class isn't even a supported option. http://symfony.com/doc/current/reference/forms/types/collection.html

so if you want to render a multiple choice list of tags you're looking for the entity type, however these are tags, and if you have any sort of decent site you'll probably have way more than a multiple choice list would be practical for. design wise you want to just have an auto-completer to show what tags already exist with the typed text as you type and then just have the user press enter to add the tag whether is exists or not. then above the auto completer you'd show the tags already added and have and x next to them that they can press on to remove the tag.

you can cheat by just having tags field in your form be a unmapped text type and use javascript to combine the tags together into a string on form submit, then in your action turn the string into your tags.

0
votes

Sorry for the delay here but would this work for you ?

 $builder
            ->add('name')
            ->add('tags','collection',array(
                    'type' => '**{{ NAME OF THE FORM }}**',
                )
            )
            ->add('save','submit');