Search code examples
phpsymfonymultiple-choice

Symfony set data to multiple choiceType


I set symfony choiceType value from inside the controller by using this:

    $editForm->get('userJobTitle')->setData($job->getJobTitle()->getId());

How to do it for multiple choiceType? the following method isn't working

 $editForm->get('userskills')->setData($job->getSkills());

where getSkills function return Doctrine collection.


Solution

  • setData() method requires array of strings which contain the values of selected options so i do:

    $usSkills = $job->getSkills()->getValues();
            $vals = array();
            foreach ($usSkills as $us){
                $vals[] = (string)$us->getId();
            }
            $editForm->get('userskills')->setData($vals); 
    

    and that solved the problem