【发布时间】:2012-04-05 09:35:30
【问题描述】:
以下代码创建一个包含多个复选框的字段。如何设置这些复选框的默认值?
new sfWidgetFormChoice(array(
"choices" => $choices,
"label" => "Label",
"multiple" => true,
"expanded" => true
));
【问题讨论】:
以下代码创建一个包含多个复选框的字段。如何设置这些复选框的默认值?
new sfWidgetFormChoice(array(
"choices" => $choices,
"label" => "Label",
"multiple" => true,
"expanded" => true
));
【问题讨论】:
您可以手动设置默认值:
$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));
如果与表单相关的对象不是新的,默认值将根据存储的值预先选择,因此您可能需要在最后一行添加$this->getObject()->isModified() 检查。
【讨论】:
如果您有一个带有 select 小部件的表单
$this->widgetSchema['example_select_export'] = new sfWidgetFormChoice(array(
'multiple' => false,
'expanded' => false,
'choices' => array("csv" => "CSV", "excel" => "Excel", "csv_mac" => "CSV mac")
);
$choices = array("csv" => "CSV", "excel" => "Excel", "csv_mac" => "CSV mac");
您可以在创建表单的操作中设置默认值,调用此
$this->form->setDefault('example_select_export','csv')
或者,如果您愿意,只需按照您希望它们显示的顺序排列您的选择
【讨论】: