Mr. B. Mr. B. - 1 year ago 100
PHP Question

Symfony: FormType, Entity, queryBuilder - how change the value of generated options?

I'd like to add the field

(string) to my

is not related to
, and should store country-codes (en, it, fr, ...) instead of the entity's ID.

I load the country codes of
to my form, but the generated select has the entity's ID as the value, the
-return as text.

How can I set
as the
s value?


public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('country', 'entity', array(
'class' => 'appBundle:Country',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')->orderBy('c.code', 'ASC');

Generated select:

<option value="1">en</option>
<option value="2">it</option>
<option value="3">fr</option>

Desired select:

<option value="en">en</option>
<option value="it">it</option>
<option value="fr">fr</option>

Thanks in advance!

Answer Source

If you are using symfony 2.7, use the choice_label option of the entity form field type. If using an older version of symfony, use the property option. More information here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download