symfony 级联下拉列表

symfony cascaded dropdown list

我正在使用 Synfony3。我有 3 个名为 "category, "services", "Profile" 的实体,它们之间的关系是 C <1-n> S <1-n> P。换句话说,1 个类别可以有多个服务,1 个服务可以有多个配置文件。

当我显示表格来补充个人资料时,我会有 1 个包含所有服务的下拉列表和 1 个包含所有类别的下拉列表

为此,我在 ProfileType 中添加了服务下拉列表,如下所示

class ProfileType extends AbstractType {
       //...
       ->add('service',        EntityType::class, array(
            'class'        => 'xxxBundle:Service',
            'choice_label' => 'name',
            'multiple'     => false,
        ))

Category 和 Profile 之间没有直接联系,我该如何添加类别

在此先感谢您的帮助

您可以调整提供的示例以处理实体(将侦听器放在单独的服务中并注入 EntityManger 以加载选择...):

如何级联select个字段

此示例说明如何根据类别 select 字段的值更改子类别 select 字段上允许的选项。 为此,您必须为客户端和服务器端动态选择子类别。

  1. 在客户端动态显示表单 客户端动态表单示例(使用 Javascript / JQuery):

    $('#category').change(function(){
        switch($(this).val()){
            case '1': // If category == '1'
                var choice = {
                    'choice1_1':'1_1',
                    'choice1_2':'1_2',
                    'choice1_3':'1_3',
                };
            break;
            case '2': // If category == '2'
                var choice = {
                    'choice2_1':'2_1',
                    'choice2_2':'2_2',
                    'choice2_3':'2_3',
                };
            break;
            case '3': // If category == '3'
                var choice = {
                    'choice3_1':'3_1',
                    'choice3_2':'3_2',
                    'choice3_3':'3_3',
                };        
            break;
        }
    
        var $subCategorySelect = $('#subCategory');
    
        $subCategorySelect.empty();
        $.each(choice, function(key, value) {
            $subCategorySelect.append($('<option></option>')).attr('value',value).text(key);
        });
    });
    

    当然,您可以通过 AJAX 电话获得选择。这不是这个例子的目的。

  2. 在服务器端使表单动态化以进行初始化和验证 服务器端动态表单示例:

    namespace AppBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    
    use Symfony\Component\Form\FormEvent;
    use Symfony\Component\Form\FormEvents;
    
    class MyBaseFormType extends AbstractType
    {
        /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('category',ChoiceType::class,array('choices'=>array(
                        'choice1'=>'1',
                        'choice2'=>'2',
                        'choice3'=>'3',
                    )))
            ;
    
            $addSubCategoryListener = function(FormEvent $event){
                $form = $event->getForm();
                $data = $event->getData();
    
                switch($data['category']){
                    case '1': // If category == '1'
                        $choices = array(
                            'choice1_1'=>'1_1',
                            'choice1_2'=>'1_2',
                            'choice1_3'=>'1_3',
                        );
                    break;
                    case '2': // If category == '2'
                        $choices = array(
                            'choice2_1'=>'2_1',
                            'choice2_2'=>'2_2',
                            'choice2_3'=>'2_3',
                        );                        
                    break;
                    case '3': // If category == '3'
                        $choices = array(
                            'choice3_1'=>'3_1',
                            'choice3_2'=>'3_2',
                            'choice3_3'=>'3_3',
                        );                        
                    break;
                }
    
                $form->add('subCategory',ChoiceType::class,array('choices'=>$choices));
            };
    
            // This listener will adapt the form with the data passed to the form during construction :
            $builder->addEventListener(FormEvents::PRE_SET_DATA, $addSubCategoryListener);
    
            // This listener will adapt the form with the submitted data :
            $builder->addEventListener(FormEvents::PRE_SUBMIT, $addSubCategoryListener);
        }
    }
    

根据对 Alsatian 和 Dbrumann 的回应,我构建了一些带有简短解释的示例 here