我的复选框列表的HTML是这样的:
<dt id="list-label">
<label for="list" class="optional">
Choose which feeds to include in the mix
</label>
</dt>
<dd id="list-element">
<label for="list-1">
<input type="checkbox" name="list[]" id="list-1" value="1">Marko Polo
</label>
<br />
<label for="list-2">
<input type="checkbox" name="list[]" id="list-2" value="2">Jeano Polo
</label>
</dd>
我正在尝试使用selected = select为数据库中值为1的那些预先填充它们。我认为以下内容可行:
$form->setDefaults(array('list-1'=>1,'list-2'=>1));
但事实并非如此。有没有办法做到这一点?
修改的
Here is my form class code:
$model = new Admin_Model_Db();
$model->setTableName('graduates');
$gradA = $model->getAllGraduates();
foreach ($gradA as $grad){
if (!empty($grad['twitter'])){
$twitter[$grad['id']] = $grad['firstname'] . ' ' . $grad['lastname'];
}
}
$list = $this->CreateElement('multicheckbox', 'list')
->setLabel('Choose which feeds to include in the mix')
->setRequired(false)
->setMultiOptions($twitter);
答案 0 :(得分:5)
试试这个:
$all_record = array('k1'=>'v1', 'k2'=>'v2');
$checked_values = aray('k1');
$checkbox = new Zend_Form_Element_MultiCheckbox('id',
array('multiOptions' => $all_records)
);
$checkbox->setValue($checked_values);
// ...
$this->addElement($checkbox);
$这当然是Zend_Form :)
答案 1 :(得分:2)
您的表单元素名称为list[]
,这意味着当您选中框并以这种方式获取其值时:
$list = $form->getElement('list')->getValue();
$ list的值将是数组(1,2)
所以逻辑上这应该有效:
$form->setDefaults(array('list'=>array(1,2));
//if not, try with strings instead of integers array('1','2')