mlwacosmos
mlwacosmos

Reputation: 4541

how to set default selected value in a liste with entity type field

Here is my code in the controller :

$entity_options = array(
    'class' => 'ACCUEILBundle:Param',
    'property' => 'description',
    'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
        return $er->createQueryBuilder('p')
                ->where("p.categorie = 5")
                ->orderBy('p.description', 'ASC');
    }
);

if($entity_type !== null) {
    $entity_options['data'] = $this->getDoctrine()->getManager()->find('ACCUEILBundle:Param', $entity_type);
}

$formBuilder->add('entity_types', 'entity', $entity_options);

$form->$formBuilder->getForm();

If I select an item in the list and submit the form, the $form->getData()['entity_types'] returns the Param Object selected.

If I want to force a default selected item with the 'data' option and display the $form->getData(), it returns an emtpy array.

Where should I get the default selected item I have just declared with the 'data' option.

Upvotes: 1

Views: 422

Answers (1)

Arthur
Arthur

Reputation: 2889

You can pass value on form creation.

$formBuilder = $this->createFormBuilder(['entity_types' => $initialValueForEntityTypeField])

Upvotes: 3

Related Questions