container->get('form.factory')->createNamedBuilde-6ren">
gpt4 book ai didi

Symfony2 : form exception - The options "class", "query_builder"不存在。已知选项有:

转载 作者:行者123 更新时间:2023-12-04 15:34:21 24 4
gpt4 key购买 nike

我有我的自定义表单,它是各种实体的组合,使用以下代码对最终用户有意义:

$form = $this->container->get('form.factory')->createNamedBuilder(null, 'form')
->add('country', 'entity', array(
'class' => 'ACME\MyBundle\Entity\Country',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('c')->orderBy('c.en_name', 'ASC');
},
'label' => '* Country',
'required' => true
),
)

即使在查阅文档 http://symfony.com/doc/current/reference/forms/types/entity.html#reference-forms-entity-choices 时,代码看起来也很好但我不断收到以下错误:

The options "class", "query_builder" do not exist. Known options are: "action",
"attr", "auto_initialize", "block_name", "by_reference", "cascade_validation",
"choice_list", "choices", "compound", "constraints", "csrf_field_name",
"csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id",
"csrf_token_manager", "data", "data_class", "disabled", "empty_data", "empty_value",
"error_bubbling", "error_mapping", "expanded", "extra_fields_message", "inherit_data",
"intention", "invalid_message", "invalid_message_parameters", "js_validation",
"label", "label_attr", "label_render", "mapped", "max_length", "method",
"multiple", "pattern", "post_max_size_message", "preferred_choices",
"property_path", "read_only", "required", "sonata_admin", "sonata_field_description",
"translation_domain", "trim", "validation_groups", "virtual"

我不知道我错过了什么,希望得到您的帮助

最佳答案

我最近使用 Symfony 3 遇到了这个问题。字段类型必须是 EntityType 才能使用 class 和 query_builder 选项。由于未知的原因,Symfony 将我的视为 ChoiceType,因此我通过声明字段类型解决了我的问题。尝试这个:

在命名空间下方添加以下用途:

use Symfony\Bridge\Doctrine\Form\Type\EntityType;

并添加这样的字段:
->add('country', EntityType::class, array(
'class' => 'ACME\MyBundle\Entity\Country',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('c')->orderBy('c.en_name', 'ASC');
},
'label' => '* Country',
'required' => true
),
)

关于Symfony2 : form exception - The options "class", "query_builder"不存在。已知选项有:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22003669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com