2017-03-09 56 views
0

我的應用程序顯示此錯誤Symfony的FormType構建錯誤

Type error: Too few arguments to function AppBundle\Form\ActualiteType::__construct(), 0 passed in /Applications/MAMP/htdocs/SyndicNous/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 90 and exactly 2 expected

我formType

類ActualiteType擴展AbstractType { /** * 布爾@var $管理 */ 私人$管理;

/** 
* @var User $user 
*/ 
private $user; 

/** 
* ActualiteType constructor. 
* @param bool|false $admin 
*/ 
public function __construct($admin = false, $user) 
{ 
    $this->admin = $admin; 
    $this->user = $user; 
} 

/** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $categories = array(
     'Travaux' => 'Travaux', 
     'Voisinage' => 'Voisinage', 
    ); 

    $builder 
     ->add('title') 
     ->add('category') 
     ->add('content') 
     ->add('datePublish') 
     ->add('category', ChoiceType::class, array(
        'choices' => $categories 
      ) 
     ); 
    if ($this->user->getResidence() != null) { 
     $builder->add('residence', EntityType::class, array(
      'class' => 'AppBundle:Residence', 
      'choices' => $this->user->getResidence(), 
     )); 
    } else { 
     $builder->add('residence', 'entity', array(
      'class' => 'AppBundle:Residence', 
      'choice_label' => 'name' 
     )); 
    }; 
} 

/** 
* @param OptionsResolver $resolver 
*/ 
public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'AppBundle\Entity\Actualite' 
    )); 
} 

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

}

你有任何想法,問題將從何而來?謝謝

+0

您試圖在不傳入任何參數的情況下實例化表單,因爲它沒有默認值 – JimL

回答

3

我不明白你要做什麼。您不需要使用構造函數將參數傳遞給您的formType。這個($ options)有buildForm方法的第二個參數。

在你的控制器,創建您的形式是這樣的:

$form = $this->createForm(ActualiteType::class, $actualite, [ 
     'admin' => $admin, 
     'user' => $user 
    ]); 

和修改formType這樣的:

class ActualiteType extends AbstractType { 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $admin = $options['admin']; // Not used ? 
     $user = $options['user']; 

     $categories = array(
      'Travaux' => 'Travaux', 
      'Voisinage' => 'Voisinage', 
     ); 

     $builder->add('title') 
       ->add('category') 
       ->add('content') 
       ->add('datePublish') 
       ->add('category', ChoiceType::class, array(
        'choices' => $categories 
       ) 
     ); 

     if ($user->getResidence() != null) { 
      $builder->add('residence', EntityType::class, array(
       'class' => 'AppBundle:Residence', 
       'choices' => $user->getResidence(), 
      )); 
     } else { 
      $builder->add('residence', 'entity', array(
       'class' => 'AppBundle:Residence', 
       'choice_label' => 'name' 
      )); 
     }; 
    } 

    /** 
    * @param OptionsResolver $resolver 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Actualite', 
      'admin'  => null, // Default 
      'user'  => null // Default 
     )); 
    } 
} 

不要忘記把你的選擇默認值configureOptions方法。