Form collection error

I am trying to take one kind of form and display it, however I need the user to download the patch download at the same time. So say 30 files to download, 30 forms per page. I get this error:

It is expected that data of the form type will be of the scalar, array, or instance \ ArrayAccess types, but this is an instance of the MS \ CoreBundle \ Entity \ Photo class. You can avoid this error by setting the "data_class" option to "MS \ CoreBundle \ Entity \ Photo" or by adding a view transformer that converts an instance of the MS \ CoreBundle \ Entity \ Photo class to a scalar, array, or \ ArrayAccess instance.

Gallery Type Code:

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, )); } 

Photo Type Code:

 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:

  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.

+8
php symfony symfony-forms
source share
1 answer

You should not pass the data_class parameter to the collection type in your GalleryType application. Or, if you want to override the default PhotoType value (which is already set, so you do not need it), you can specify it in the parameter array as follows:

 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 there is a default data_class option 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 the 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() ); } 
+11
source share

All Articles