首页 > 解决方案 > 如何在 Symfony 中使用'choice_name'?

问题描述

我有一个带有西里尔字母名称的复选框列表。它生成

<input type="checkbox" id="region_form_name_22" name="region_form[name][]" value="22">"

我想输入$query = $request->query->get("region_form");一个字符串值(如“Алтайская область”。

我尝试使用“choice_name” ,但出现错误

"The name "Алтайский край" contains illegal characters. Names should start with a letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":")."

$builder->add('name', EntityType::class, [
            'class' => Region::class,
            'choice_label' => 'name_with_type',
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->orderBy('u.name_with_type', 'ASC');
            },
           'choice_name' => function ($entity) {
               return $entity->getNameWithType();
           },
            'multiple' => true,
            'expanded' => true,
        ])
            ->add('save', SubmitType::class, array('label' => 'Найти'))
        ;

如何在查询中获取不是“id”,而是“name”?

标签: symfony

解决方案


推荐阅读