Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

symfony

I have my custom form which is a combination of various entities into what makes sense to the end user with 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 fine even when consulting 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 am missing and would appreciate your help

like image 939
Masinde Muliro Avatar asked Feb 25 '14 02:02

Masinde Muliro


1 Answers

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

Add the following use below the namespace:

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

and add the field like this:

->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
    ),
)
like image 134
sgaith Avatar answered Oct 14 '22 02:10

sgaith