Reputation: 6471
With my formBuilder I am building a choice field:
$options = [
'attr' => array('class' => 'form-control'),
'data' => '2',
'mapped' => false,
'label' => $label,
'data_class' => null,
];
if ($type == 'choice') {
$options['attr'] = array('class' => 'form-control select2');
$options['class'] = 'App\Entity\Options';
$options['choice_label'] = 'name';
$options['query_builder'] = function (EntityRepository $er) use ($fieldId) {
return $er->createQueryBuilder('f')
->leftJoin('f.fields', 'fi')
->where(":id MEMBER OF f.fields")
->setParameter(':id', $fieldId);
};
$options['data'] = '2';
$formBuilder->add($id, ChoiceType::class, $options);
}
It works well, a choice field is created with all my options. The problem is, that I want the default selected option to be 2
.
And this doesn't work. Always the first option is selected. So the default selected option is always green
. But I need it to be blue
. This is selectbox, that is created:
<select class="form-control select2 select2-hidden-accessible" tabindex="-1" aria-hidden="true">
<option value="1">green</option>
<option value="2">blue</option>
<option value="3">red</option>
</select>
Upvotes: 0
Views: 534
Reputation: 47584
You are setting data to a literal. Set it to an object reference:
$options['data'] = $entityManager->getReference(YourEntity::class, 2);
Upvotes: 1