Jestep
Jestep

Reputation: 985

Symfony 2.8 Form EntityType Returns Single Value

Just trying to get a select field populated from a database table. This is just a simple table with a primary key column and another column named type.

Just for testing, the example table consists of:

id    type
1     Sample 1
2     Sample 2
3     Sample 3

When I create a form:

$builder
       ->add('account_type', EntityType::class, array(
            'class' => 'AppBundle:AppAccountTypes',
            'choice_label' => 'type'
        ));

My select dropdown simply repeats the first entry 3 times.

<select id="add_account_form_account_type" name="add_account_form[account_type]" class="form-control">
<option value="1">Sample 1</option>
<option value="1">Sample 1</option>
<option value="1">Sample 1</option>
</select>

For testing sake, the controller is just using:

$account = new Account();

$form = $this->createForm(new AddAccountForm(), $account);

return $this->render('account/new.html.twig', array(
    'page_title' => 'Create Account',
    'form' => $form->createView()
));

Twig template:

{% extends 'base.html.twig' %}

{% block body %}
    <h1>{{ page_title }}</h1>
    {{ form_start(form) }}
    {{ form_widget(form) }}
    {{ form_end(form) }}
{% endblock %}

What am I missing here?

Upvotes: 0

Views: 541

Answers (1)

Maulik Savaliya
Maulik Savaliya

Reputation: 1260

May be you need to use below code:

$builder
   ->add('account_type', EntityType::class, array(
        'class' => 'AppBundle:AppAccountTypes(Your Entity Class)',
        'mapped' => false,
        'choice_label' => 'type'
    ));

Because as i see in your code there is no field which name is account_type, May be this is the problem.

Change your controller as below:

$form = $this->createForm(AddAccountForm::class (Your form class), $account);

Upvotes: 3

Related Questions