【问题标题】:Symfony fill ChoiceType with an arraySymfony 用数组填充 ChoiceType
【发布时间】:2017-07-19 20:04:10
【问题描述】:

我正在尝试用数组填充我的 ChoiceType,但看起来它填充的是 ID 而不是值。表单已正确显示,但选项是“0”、“1”...而不是数组中的名称。

这是我的控制器:

$categories = $this->getDoctrine()->getRepository('myBundle:Category')->findAll();

    $techChoices = array();
    $i = 0;
    foreach($categories as $t) {
        $techChoices[$i] = $t->getName();
        $i = $i + 1;
    }

    $formOptions = array('categories' => $techChoices);


    $document = new Document($categories);
    $form = $this->createForm(DocumentType::class, $document, $formOptions);

这是我的 buildForm:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('file', FileType::class)
        ->add('file_name', TextType::class)
        ->add('file_description', TextType::class)
        ->add('file_group', ChoiceType::class, array(
            'choices'  =>  $options['categories'],
        ));
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'categories' => array(),
    ));
}

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    根据您的 Symfony 版本(自 2.8 起),您构建选择数组的方式有误。

    来自3.3 documentation

    ...其中数组键是项目的标签,数组值是项目的值。

    【讨论】:

    • 我正在像“选择”示例一样,我只是想传递一个现有数组而不是声明一个新数组。
    【解决方案2】:

    在您的案例中显示类别的正确方法是使用 EntityType,这将释放您的代码混乱。您不再需要获取/传递类别来形成。

    public function buildForm(FormBuilderInterface $builder, array $options) {
            $builder
                    ->add('file', FileType::class)
                    ->add('file_name', TextType::class)
                    ->add('file_description', TextType::class)
                    ->add('file_group', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, array(
                        // query choices from this entity
                        'class' => 'AppBundle:Category',
                        'choice_label' => 'name', 
                    ))
    
            ;
        }
    

    【讨论】:

      【解决方案3】:

      如果您想直接在表单选择类型中使用数组,请参阅https://symfony.com/doc/current/reference/forms/types/choice.html#example-usage,或者如果您想使用表(实体)中的数据,请参阅https://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage

      回答你的问题是数组格式应该是这样的

      ['data_to_be_seen1' => value1(id), 'data_to_be_seen2' => value2(id),...]

      (见第一个链接),

      一切顺利

      【讨论】:

      • 非常感谢,第二个链接正是我需要的。现在可以使用了
      【解决方案4】:

      你可以直接这样做:

      $builder
          ->add('file', FileType::class)
          ->add('file_name', TextType::class)
          ->add('file_description', TextType::class)
          ->add('file_group', ChoiceType::class, array(
              'choices'  =>  'here you pass your categories entities directly',
              'choice_label' => 'name',
          ));
      

      这样,它会单独做映射

      【讨论】:

      • “在这里你直接传递你的类别实体”是什么意思?我尝试了数组
      • 为什么要重新创建一个数组?只需传递 findAll 结果
      • 我这样做是因为我试图跟随遇到同样问题的人的答案,但是当我传递 findAll 结果时它是一样的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      相关资源
      最近更新 更多