0

I want to add <select> with selected line in my Form using Doctrine2 / Symfony2

How can i make one value selected with entity return?!

This is my code :

$content = new Content();
    $form = $this->createFormBuilder($content)
            ->add('idcontent', 'entity', array(
                'class' => 'SdmSiteInterBundle:Activite',
                'query_builder' => function($repository) { 
                    return $repository->createQueryBuilder('c')->orderBy('c.name', 'ASC');
                },
                'property' => 'name',
                'data' => 82,
            ))
            ->getForm();

To more explain, i want this result :

<select>
<option value="25">activity name 01</option>
**<option value="82" selected="selected">activity name 02</option>**
<option value="214">activity name 03</option>
.....
</select>
chakroun yesser
  • 1,407
  • 1
  • 12
  • 18
  • Based on what i'm reading, you'd have to pass the entity in question as the data option, could be wrong. – Moylin Aug 06 '13 at 15:05
  • how?! it juste accept "QueryBuilder" format in query_builder option – chakroun yesser Aug 07 '13 at 07:16
  • Take a look at this similar topic: http://stackoverflow.com/questions/11999745/how-to-set-up-default-value-in-symfony2-select-box-with-data-from-database/12000289#12000289 [1]: http://stackoverflow.com/questions/11999745/how-to-set-up-default-value-in-symfony2-select-box-with-data-from-database/12000289#12000289 – Thierry Aug 27 '13 at 07:41

1 Answers1

0

Honestly, I didn't fully understand your question, but if you want to know how to set the value of a "default" combobox, then just set the property as you normally would to initialized your object.

$content = new Content();
$content->setActivite($someActivite);

Also, in your example, you're creating a form field for the idcontent field of the type $content when you probably want to do the following:

$content = new Content();
$content->setActivite($someActivite);

$form = $this->createFormBuilder($content)
        ->add('activite', 'entity', array(
            'class' => 'SdmSiteInterBundle:Activite',
            'query_builder' => function($repository) { 
                return $repository->createQueryBuilder('c')->orderBy('c.name', 'ASC');
            },
            'property' => 'name'
        ))
        ->getForm();

See http://symfony.com/doc/current/reference/forms/types/entity.html for more details

saamorim
  • 3,855
  • 17
  • 22