Symfony set data to multiple choiceType

旧街凉风 提交于 2019-12-12 04:55:23

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/45376155/symfony-set-data-to-multiple-choicetype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!