2017-06-07 1 views
1

Je voudrais savoir comment transmettre mon entité correctement et une autre variable dans ma fonction createForm?Symfony 3 FormType - Entité à travers le tableau d'options

Permettez-moi de vous expliquer, je voudrais faire:

$repo = $em->getRepository('ProjectBundle:Organisation\Organisation'); 
      $list = $repo->children($this->getUser()->getOrganisation()); 
      $form = $this->createForm('ProjectBundle\Form\Robot\RobotType', array($entity,$list)); 

Je dois passer à mon FormType une autre variable, donc j'utiliser un tableau directement.

Mon FormType:

<?php 

namespace ProjectBundle\Form\Robot; 

use ProjectBundle\Entity\Robot\Robot; 
use ProjectBundle\Form\User\UserType; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

class RobotType extends AbstractType 
{ 
    /** 
    * {@inheritdoc} 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('serial_number') 
      ->add('shell_color',ChoiceType::class,array(
       'choices' => Robot::getArrayShellColor() 
      )) 
      ->add('tray1Color',ChoiceType::class,array(
       'choices' => Robot::getTrayColor() 
      )) 
      ->add('tray2Color',ChoiceType::class,array(
       'choices' => Robot::getTrayColor() 
      )) 
      ->add('tray3Color',ChoiceType::class,array(
       'choices' => Robot::getTrayColor() 
      )) 
      ->add('hasBattery') 
      ->add('volume') 
      ->add('organisation',null,array('choices' => $options['data'][1])); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'ProjectBundle\Entity\Robot\Robot' 
     )); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function getBlockPrefix() 
    { 
     return 'projectbundle_robot_new'; 
    } 


} 

Ici, je dois obtenir ma liste de variables $, pour le domaine de l'organisation, il est donc $options['data'][1].

Mais j'ai une erreur, que je comprends, mais je ne sais pas comment corriger:

The form's view data is expected to be an instance of class ProjectBundle\Entity\Robot\Robot, 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 ProjectBundle\Entity\Robot\Robot. 

Ceci est normal parce que maintenant je passe dans un tableau, il est ma $options['data'][0] variable qui contient mon objet maintenant .

Comment faire? Merci

+0

Vérifiez EntityType et le paramètre query_builder – Koalabaerchen

Répondre

1

Vous devez transmettre l'entité ProjectBundle \ Entity \ Robot \ Robot à la méthode createForm en tant que deuxième paramètre et un tableau d'options en tant que troisième paramètre.

$form = $this->createForm('ProjectBundle\Form\Robot\RobotType', $entity, array($list));