我正在做一个Zend框架的博客。此时此刻,我正在制作编辑一篇博文的页面。
我有一个多重选择元素。它把他的数据从数据库中取出来。它是一个带有标签的列表。当我编辑一篇博客文章时,它会在我的表单中显示一个列表,其中包含了所有可能的标签。现在,我想设置一些值。(在数据库中属于文章的标签)。
这可能吗?
在我的表单中,我这样做是为了使多重选择与值
//create form element
$tags = $this->createElement('multiselect','tags');
//get all tags out DB
$tags_model = new Application_Model_DbTable_Tags();
$tags_array = $tags_model->getAll();
//fill the element with the tags
foreach ($tags_array as $tag){
$tags->addMultiOption($tag['id'], $tag['name']);
}
控制器,我调用表单:它从blogpost中获取数据并将其填充到表单中。
$blogpost = new Application_Model_DbTable_Blogposts();
$data = $blogpost->load($id);
$form->populate($blogpost->load($id));
解决方案:
$tags->setValue(array(1,4));
我必须把解决方案写在这里,因为:
"Oops! Your answer couldn't be submitted because:
Users with less than 100 reputation can't answer their own question for 8 hours after asking. You may self-answer in 7 hours. Until then please use comments, or edit your question instead."
请参阅MultiSelect文档。使用所有值调用addMultiOptions(),然后对数据库中所选值的子集调用setMultOptions()。