我有自定义表单,它是各种实体的组合,使用以下代码对最终用户有意义:
$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"
我不知道我错过了什么,并感谢你的帮助
答案 0 :(得分:3)
我最近使用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
),
)