Symfony2: form exception - Parameters "class", "query_builder" do not exist. The following options are known:

I have my own custom view, which is a combination of various objects, which makes sense to the end user using the following code:

$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 ), ) 

The code seems great even when reviewing the documentation http://symfony.com/doc/current/reference/forms/types/entity.html#reference-forms-entity-choices , but I keep getting the error below:

 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" 

I don’t know what I don’t have, and I will be grateful for your help.

+6
source share
1 answer

I recently ran into this problem using Symfony 3. The field type must be EntityType to use class parameters and query_builder. For unknown reasons, Symfony saw mine as ChoiceType, so I solved my problem by declaring the field type. Try the following:

Add the following usage under the namespace:

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

and add the following field:

 ->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 ), ) 
+2
source

All Articles