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

时间:2012-03-20 21:04:33

标签: symfony-1.4 symfony-forms

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

new sfWidgetFormChoice(array(
 "choices" => $choices,
 "label" => "Label",
 "multiple" => true,
 "expanded" => true
));

2 个答案:

答案 0 :(得分: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));

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

答案 1 :(得分:0)

如果您的表单中包含选择

的窗口小部件
$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')

或者,如果您愿意按照您希望他们展示的顺序订购您的选择