为 symfony 选择字段设置默认数据,将 multiple 设置为 true

set default data for symfony choice field with multiple set to true

我正在尝试呈现一个带有复选框的选择字段,我希望在编辑模式下,这具有用户之前检查过的数据,这意味着一种设置默认数据的方式。下面的代码不起作用。有人可以帮忙吗?谢谢

$days = array("monday" => "monday","tuesday" => "tuesday");
$builder->add('channels', 'choice', array(
            'choices' => array(
                'days'  => $days,                
            ),
            'multiple' => true,
            'expanded' => true,
            'required' => true,
            'data' => array("choices" => array("days" => array("monday")))
        ));

删除数据数组中的choices键:

$builder->add( 'channels', 'choice', array( ... 'data' => array("monday") ) );

我认为这应该可行:

$builder->add(
    'channels',
    'choice',
    array(
        ...
        'data' => array("monday"),
    )
)

这个对我有用(Symfony 2.3.x):

$days = array("monday" => "Monday","tuesday" => "Tuesday","wednesday" => "wednesday","thursday" => "thursday");
    $builder->add('channels', 'choice', array(
        'choices' => array(
            'days'  => $days,                
        ),
        'multiple' => true,
        'expanded' => true,
        'required' => true,
        'data' => array("wednesday","thursday","tuesday")
    ));  

注意:使用键索引设置数据而不是值。 "Tuesday" 不会像 "tuesday" 那样在这里工作。