表单选项是必需的,不是必需的

时间:2018-11-12 09:42:46

标签: php forms symfony

我有一个大问题,经过数小时的研究,我找不到任何答案...

我有 2种形式,我要互相称呼。
当我在表单中输入“ entity_manager”选项时,会显示以下错误消息:

  

选项“ entity_manager”不存在。

但是,当我删除此选项时,我将显示以下内容:

  

缺少必需的选项“ entity_manager”。

有我的代码:

CandidatureController.php

$candidature = new Candidature();
$formCandidature = $this->createForm('PDF\RecrutementBundle\Form\CandidatureType', $candidature, array('entity_manager' => $entityManager, 'form' => 'candidature'));
$formCandidature->handleRequest($request);
$formOrientation = $this->createForm('PDF\RecrutementBundle\Form\CandidatureType', $candidature, array('entity_manager' => $entityManager, 'form' => 'orientation'));

CandidatureType.php

public function buildForm(FormBuilderInterface $builder, array $options) {
    $this->em = $options['entity_manager'];
    $form = $options['form'];

    if ($form == 'candidature') {
        $builder->add('caCommentairesituation', TextareaType::class)
                ->add('caCommentaire', TextareaType::class)
                ->add('profils', CollectionType::class, array('entry_options' => ['entity_manager' => $this->em], 'entry_type' => ProfilType::class,'allow_add' => true, 'allow_delete' => true,'prototype' => true))
                ->add('fkMobilitegeographique', EntityType::class, array('class'=>'PDF\RecrutementBundle\Entity\Mobilitegeographique', 'choice_label'=>'mgLibelle'))
                ->add('fkSituationprofessionnelle',EntityType::class, array('class'=>'PDF\RecrutementBundle\Entity\Situationprofessionnelle', 'choice_label'=>'spLibelle'))
                ->add('fkTypecandidature', EntityType::class, array('class'=>'PDF\RecrutementBundle\Entity\Typecandidature', 'choice_label'=>'tcLibelle'))
                ->add('fkProvenance', EntityType::class, array('class'=>'PDF\RecrutementBundle\Entity\Provenance', 'choice_label'=>'prLibelle'));
    } else {
        $builder->add('orientations', CollectionType::class, array('entry_options' => ['entity_manager' => $this->em], 'entry_type' => OrientationType::class, 'allow_add' => true, 'allow_delete' => true, 'prototype' => true));
    }
}

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'PDF\RecrutementBundle\Entity\Candidature',
        'form' => false
    ));
    $resolver->setRequired('entity_manager');
}

OrientationType.php

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('orLibelle', TextType::class)
            ->add('orDescription', TextType::class)
            ->add('orArchive')
            ->add('orDiffusion')
            ->add('fkCategorieorientation', EntityType::class, array('class'=>'PDF\RecrutementBundle\Entity\CategorieOrientation', 'choice_label'=>'coLibelle'))
            ->add('candidatures', CollectionType::class, array('entry_type' => CandidatureType::class, 'allow_add' => true, 'allow_delete' => true));
}

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'PDF\RecrutementBundle\Entity\Orientation'
    ));
}

那我该如何解决呢?

PS :对不起,我的英语不好。

1 个答案:

答案 0 :(得分:0)

在OrientationType.php中,CollectionType“ candidatures”的条目类型为CandidatureType,但是您没有定义“ entry_options”,而是传递给条目类型的选项...

尝试替换

 ->add('candidatures', CollectionType::class, array('entry_type' => CandidatureType::class, 'allow_add' => true, 'allow_delete' => true))

附带:

  ->add('candidatures', CollectionType::class, array(
    'entry_type' => CandidatureType::class, 
    'entry_options' => [
       'entity_manager' => $this->em
    ],
    'allow_add' => true, 
    'allow_delete' => true)
);

当然,您需要首先在OrientationType中将$ em定义为entity_manager,与在CandidatureType中使用setRequired()定义它的方法相同。

相关问题