0

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?

5
  • What does your controller and twig look like?
    – Kep
    Commented Mar 21, 2017 at 21:48
  • Updated, simplified everything just for testing sake. I'm sure I'm missing something really obvious, but I can't see it.
    – Jestep
    Commented Mar 21, 2017 at 21:53
  • Looks fine from what I can see... Probably try posting the entity and repository aswell - although I doubt it's anything obvious there
    – Kep
    Commented Mar 21, 2017 at 21:57
  • 1
    Very strange. Everything looks good. Maybe post your AppAccountTypes.php Entity file.
    – Alvin Bunk
    Commented Mar 21, 2017 at 22:26
  • Figured it out by looking at the AppAccountTypes.php. There's a bug or feature in doctrine:mapping:import. The id column for the ap_account_types table id was using tinyint. Symfony generated an entity with a boolean instead of an integer field. This: stackoverflow.com/questions/14681197/…
    – Jestep
    Commented Mar 22, 2017 at 13:31

1 Answer 1

3

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.