Symfony 1.4 - 如何在sfWidgetFormChoice上设置默认值(复选框)

afi*_*ina 4 symfony-1.4 symfony-forms

以下代码创建一个具有多个复选框的字段.如何设置这些复选框的默认值?

new sfWidgetFormChoice(array(
 "choices" => $choices,
 "label" => "Label",
 "multiple" => true,
 "expanded" => true
));
Run Code Online (Sandbox Code Playgroud)

Mik*_*ell 5

您可以手动设置默认值:

$countries = array(
    'Spain',
    'England',
    'France'
);

$this->widgetSchema['countries'] = new sfWidgetFormChoice(array('choices' => $countries, 'multiple' => true, 'expanded' => true));

// Form will render with Spain and England pre-selected
$this->widgetSchema['countries']->setDefault(array(0,1));
Run Code Online (Sandbox Code Playgroud)

如果与表单相关的对象不是新对象,则将根据存储的值预先选择默认值,因此您可能需要$this->getObject()->isModified()在最后一行附近添加检查.