Selecting multiple items by default in sfWidgetFormDoctrineChoice (symfony)

Using Symfony and Doctrine, I have a list with multiple choices. Multiple defaults should be generated based on Doctrine query.

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

      

This gives me a list of all the items in my folder; however, I want them to be preselected by the folder list. 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

passed, I want to select item a

item b

and item c

, but I want item d

also item e

in the list, but not selected (but can be selected)

+2


a source to share


1 answer


If you are using 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 values ​​to be fetched

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

      

+2


a source







All Articles