繁体   English   中英

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

[英]Form option required AND not required

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

我有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 :对不起,我英语不好。

在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()定义它的方法相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM