如何关联symfony表单中的两个输入字段


How to relate two input field in a symfony 3 form

请告诉我如何才能做到这一点。我有两个选择字段(实体),如类别和技能。我希望技能领域是依赖于类别,即,如果一个类别被选择的技能的选项应该是与该类别相关的技能,但如果没有类别被选择,该领域的选项应该是所有的技能。

注意下面的代码只是为了说明目的

   public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        if ( a value is selected from
             (->add('category', EntityType::class, array(
            'class'=>'AppBundle:Category',
            'choice_label'=>'name'
             ))) )
        {
                present skills based on the selected category as
                ( ->add('skill', EntityType::class, array(
                    'class'=>'AppBundle:Skill',
                    'choice_label'=>'name',
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('s')
                            ->select('s')
                            ->where('c = :category')
                            ->setParameter('category', $value selected from category);
                    },
                )))
}else{
 ->add('skill', EntityType::class, array(
    'class'=>'AppBundle:skill',
    'choice_label'=>'name'
    ))

}

您需要使用表单事件订阅者:

https://symfony.com/doc/current/form/dynamic_form_modification.html