views:

326

answers:

1

Using Symfony and Doctrine, I have a multi-select list box. The multiple default values need to be generated based on a Doctrine query.

$this->setWidgets(array(
  'folders' => new sfWidgetFormDoctrineChoice(array(
    'model' => 'FolderItem',
    'order_by' => array('name', 'asc'),
    'multiple' => true,
    'query' => FolderItemTable::getUserInstance($user),
  ))
));

This gets me a list of all my folder items; however, I want them pre-selected by a list of Folders. For example, if I have Folder 1 containing item a item b and item c and Folder 2 containing item d and item e; if Folder 1 is passed in I want item a item b and item c to be selected, but I want item d and item e in the list but not selected (but selectable)

+1  A: 

If your are using a Doctrine relation to populate the list you can do something like this in your form class

  $this->setDefault('folders', $this->object->Users->getPrimaryKeys());

You can also pass an array with the values to be selected

  $this->setDefault('folders', array(125,2049,12));
Benoit