2012-06-27 9 views
8

Estoy tratando de tomar un tipo de formulario y mostrarlo en cualquier momento que necesite para que el usuario cargue un parche al mismo tiempo. Así que digamos 30 archivos para cargar, 30 formularios en la página. Estoy recibiendo este error:Error de recopilación de formularios

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.

El código Galería de tipos es:

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

el código de tipo de fotos es:

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

Mi función del controlador es:

 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() 
      ); 


    } 

Cualquier ayuda sería genial.

Respuesta

11

No debe pasar la opción data_class al collection type en su GalleryType. O bien, si desea anular predeterminado del fototipo (que ya está establecida, por lo que no debería tener a), puede especificar que en la matriz de opciones, así:

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

Asegúrese de que hace tiene una opción predeterminada data_class establecida en su "GalleryType", debería ser un álbum, parece.

Además, en su controlador no está creando correctamente el formulario. Debe llamar al setData() con el tipo de datos del formulario, en este caso, un álbum.

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() 
     ); 
} 
Cuestiones relacionadas