标题几乎说明了一切,但这里是我的代码片段来说明我正在尝试做的事情。
这是表单类型
//[...]
class ActivityFilterType extends AbstractType
{
//[...]
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('filter_checkbox', 'choice', array(
'label' => $this->translator->trans('form.label.sexFilter'),
'choices' => array(1 => ucfirst($this->translator->transChoice('sex.female', 2)), 2 => ucfirst($this->translator->transChoice('sex.male', 2))),
'multiple' => true,
'expanded' => true,
'required' => false,
'empty_value' => false,
/*
// those ones didn't make it
'attr' => array( 1=>array('checked' => 'checked'), 2=>array('checked' => 'checked') ),
'preferred_choices' => array(1, 2),
*/
));
}
//[...]
}
和表格模板
<div class="filter-message">
<div class="select-group">
{{ form_label(form.filter_dropdown) }}
<div class="input control-group filter" >
{{ form_widget(form.filter_dropdown) }}
</div>
</div>
<div class="checkbox">
{{ form_label(form.filter_checkbox) }}
{{ form_widget(form.filter_checkbox, {'attr':{'checked':checked}}) }}
<!-- that didn't do it neither -->
</div>
</div>
谢谢