我的 zend 表单中有一个(感兴趣的)多选框,我在添加用户时从中选择多个选项。
现在当我编辑这个用户时,我需要设置一些默认选择的选项,(我在添加用户时选择的)。
我怎样才能做到这一点?我在 Controller 的更新操作中使用了 populate(Array),但这不起作用。
这是用户添加/编辑表单中多选框的代码:
$interests = new Zend_Form_Element_Multiselect('interest');
$days->setLabel('Interests')
->addMultiOptions($user_interests)
->setRequired(true)
->addValidator('NotEmpty');
在表单中添加“兴趣”选项时,$user_interests
数组为:
array(1=>'Blogging', 2=>'Swimming', 3=>'Cricket', 4=>'Yoga')
我在添加用户时选择了前 2 个兴趣。
现在在编辑时,我从数据库查询中获取用户数据。这个数据数组是用来填充表单的,这个数组结构是这样的:
Array (
[username] => john
[user_dob] => 1981-03-12
[email] => john@gmail.com
[interest] => Array ( [0] => 1 [1] => 2 )
)
如您所见,应该在我的编辑表单中选择兴趣“博客”和“游泳”。但我看到只有“游泳”选项被选中。
最佳答案
$interest_data = $users->getUserInterests($id);
foreach($interest_data as $r)
{
$interests[$r['interest_id']] = $r['interest'];
}
$this->view->interests_selected = $interests;
在这之后我们可以在 Controller 中像这样
$form->interest_id->setValue($interests);
希望这对某人有帮助
关于php - 编辑表单时填充 zend 多选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5780207/