Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form Collection Error

I am trying to take one form type and display it however times i need for the user to upload a patch upload at one time. So say 30 files to uploaded, 30 forms on the page. I am receiving this 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 MS\CoreBundle\Entity\Photo. You can avoid this error by setting the "data_class" option to "MS\CoreBundle\Entity\Photo" or by adding a view transformer that transforms an instance of class MS\CoreBundle\Entity\Photo to scalar, array or an instance of \ArrayAccess.

The Gallery Type code is:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('photo', 'collection', array(
        'type' => new PhotoType(),
        'allow_add' => true,
        'data_class' => 'MS\CoreBundle\Entity\Photo',
        'prototype' => true,
        'by_reference' => false,
    ));
}

The Photo Type code is:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('description', 'text', array('label' => "Title:", 'required' => true))
                ->add('File')
                ->add('album', 'entity', array(
                    'class' => 'MSCoreBundle:Album',
                    'property' => 'title',
                    'required' => true,
                    'query_builder' => function(EntityRepository $er)
                    {
                        return $er->createQueryBuilder('a')
                            ->orderBy('a.title', 'ASC');
                    },
                ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MS\CoreBundle\Entity\Photo',
        ));
    }

My Controller function is:

     public function newAction($count)
        {
            for($i = 1; $i <= $count; $i++) {
                $entity = new Photo();
            }

            $form = $this->container->get('ms_core.gallery.form');
            $form->setData($entity);

            return array(
                'entity' => $entity,
                'form' => $form->createView()
            );


  }

Any help would be great.

like image 339
Dave Mascia Avatar asked Jun 27 '12 20:06

Dave Mascia


1 Answers

You should not pass the data_class option to the collection type in your GalleryType. Or, if you do want to override the PhotoType's default (which is already set, so you shouldn't have to), you can specify it in the options array like so:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('photo', 'collection', array(
        'type' => new PhotoType(),
        'allow_add' => true,
        'options' => array('data_class' => 'MS\CoreBundle\Entity\Photo'),
        'prototype' => true,
        'by_reference' => false,
    ));
}

Make sure you do have a default data_class option set in your "GalleryType", it should be an Album, it seems.

Also, in your controller you are not creating the form correctly. You need to call setData() with the data type of the form, in this case an Album.

public function newAction($count)
{
        $album = new Album();
        for($i = 1; $i <= $count; $i++) {
            $album->addPhoto(new Photo());
        }

        $form = $this->container->get('ms_core.gallery.form');
        $form->setData($album);

        return array(
            'entity' => $album,
            'form' => $form->createView()
        );
}
like image 58
MDrollette Avatar answered Oct 28 '22 20:10

MDrollette