【问题标题】:How to set the choice option name in Symfony 4.1?如何在 Symfony 4.1 中设置选择选项名称?
【发布时间】:2018-08-22 14:40:51
【问题描述】:

看了很多遍Symfony choices之后,我不明白如何在我的下拉列表中设置选项的名称?

class CategoryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('parentCategory', EntityType::class, array(
                'class' => Category::class,
                'query_builder' => function (EntityRepository $er) {
                    $er->createQueryBuilder('c')
                        ->orderBy('c.name', 'ASC');
                },
                'choice_label' => 'parent category',
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Category::class,
        ]);
    }
}

【问题讨论】:

  • 如果parentCategory 是一个属性并且您的实体中有getParentCategory() - 根据手册然后'choice_label' => 'parentCategory',
  • 不幸的是它没有帮助,仍然只有选项中的值
  • 然后迭代选择并输出您需要的内容,在手册中有一个示例
  • @u_mulder,对不起,但我不明白如何实现这个 =(
  • 一个例子,你读了吗?将choice_attr 设置为具有示例中描述的参数的函数。如果您不知道它们的值,则使用 print_r 输出参数。

标签: php symfony-forms symfony4


【解决方案1】:

'choice_label' => 'parent category'改成'choice_label' => 'name'解决问题,然后输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多