Symfony - 一个实体属性的多个字段

Symfony - Multiple Fields for one Entity Attribute

我有一个实体属性的三个三个 select 字段。如下图所示。

有没有办法检测使用了 select 字段中的哪一个?然后获取它的值并映射到对应的属性?

是否可以将参数发送到表单类型(在此示例中 TestType,请参见下文)。我正在尝试使其通用并可重复用于其他属性。

这是我目前的情况。

MyForm.php

<?php

namespace MyBundle\Form;

use MyBundle\Form\Type\TestType;
use ..etc

class MyForm extends AbstractType
{

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

        $builder
            ->add('title',      TextType::class)
            ->add('D1',         TestType::class);

    }

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


    public function getBlockPrefix()
    {
        return 'mybundle_project';
    }


}

TestType.php

<?php

namespace MyBundle\Form\Type;

use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
use ..etc

class TestType extends AbstractType
{
    /*
     * private $myArray1;
     * private $myArray2;
     * private $myArray3; numberOfSeletcs
     * private $numberOfSeletcs;        
    Secondary Question: Is it possible to send these values as parameters?

    public function __construct($array1, $array2, $array3, $n)
    {
        $this->myArray1= $array1;
        $this->myArray2= $array2;
        $this->myArray3= $array3;
        $this->numberOfSeletcs= $n;

    }


    */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $myArray1 = array('label1'=>'','Value1'=>'Value1', 'Value2'=>'Value2','Value3'=>'Value3');
        $myArray2 = array('label2'=>'', 'Value4'=>'Value4','Value5'=>'Value5');
        $myArray3 = array('label3'=>'', 'Value6'=>'Value6','Value6'=>'Value6');

        $builder
            // ...
            ->add('H1', 'choice',  array(
                'choices' => $myArray1,
                'choice_attr' => ['label1' => ['disabled selected hidden'=>'']]))
            ->add('H2', 'choice',  array(
                'choices' => $myArray2,
                'choice_attr' => ['label2' => ['disabled selected hidden'=>'']]))
            ->add('H3', 'choice',  array(
                'choices' => $myArray3,
                'choice_attr' => ['label3' => ['disabled selected hidden'=>'']]));
    }


}

谢谢。

要检测使用了哪个 select 字段,您必须使用 Javascript。如您所知,Symfony 是一个在服务器端工作的 PHP 框架,需要在客户端检测事件 javascript。对于将参数传递给您的表单类型,您可以在

中找到答案