我想用多个复选框填充表单,并且多个复选框被组合在一起,所以如果我选中了一个多复选框,我将全部查看它们。正常填充不起作用。它只是给我一个复选框。这是我的代码。
public function addEditAction() {
$id = (int) $this->_request->getParam('id');
$request = $this->getRequest();
$form = new Admin_Form_Tab();
$db = Zend_Db_Table::getDefaultAdapter();
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
if (isset($id) && $id != "") {
$gettag = $form->getValue('tag_id');
$gettags = count($gettag);
try {
//shift the orders to
$select = $db->select()->from(array('t' => 'tab'), array('t.id',
't.title',
't.tab_position',
't.order',
't.is_active',
't.is_deleted'))->where('id = ?', $id);
$currentTab = $db->fetchRow($select);
$var3 = array('tab.order' => new Zend_Db_Expr('tab.order - 1'));
$var4 = array('tab.order >= ' . $currentTab['order'], 'is_active=1', 'is_deleted=0', 'tab_position=1');
$db->update('tab', $var3, $var4);
$var = array('tab.order' => new Zend_Db_Expr('tab.order + 1'));
$var2 = array('tab.order >= ' . $form->getValue('order'), 'is_active=1', 'is_deleted=0', 'tab_position=1');
$db->update('tab', $var, $var2);
$db->delete('tab_tag', array('tab_id = ?' => $id));
foreach ($gettag as $value) {
$db->insert('tab_tag',
array(
'tag_id' => $value,
'tab_id' => $id
));
}
$db->update('tab', array(
'title' => $form->getValue('title'),
'body' => $form->getValue('body'),
'is_active' => $form->getValue('is_active'),
'banner_link' => $form->getValue('banner_link'),
'tab_path' => $form->getValue('tab_path'),
'link_open' => $form->getValue('link_open'),
'tab_position' => $form->getValue('tab_position'),
'tab_parent' => $form->getValue('tab_parent')
),
array('id =?' => $id));
$this->flash('Tab Updated', 'admin/tab');
} catch (Exception $e) {
$this->flash($e->getMessage(), 'admin/tab');
}
} else {
try {
$formValues = $form->getValues();
$formValues['created_by'] = 1;
$formValues['created_date'] = date('Y/m/d H:i:s');
$formValues['is_deleted'] = 0;
$var = array('tab.order' => new Zend_Db_Expr('tab.order + 1'));
$var2 = array('tab.order >= ' . $form->getValue('order'), 'is_active=1', 'is_deleted=0', 'tab_position=1');
$db->update('tab', $var, $var2);
foreach ($gettag as $value) {
$db->insert('tab_tag',
array(
'tag_id' => $value,
'tab_id' => $id
));
}
$db->insert('tab', array(
'title' => $form->getValue('title'),
'body' => $form->getValue('body'),
'is_active' => $form->getValue('is_active'),
'banner_link' => $form->getValue('banner_link'),
'tab_path' => $form->getValue('tab_path'),
'link_open' => $form->getValue('link_open'),
'tab_position' => $form->getValue('tab_position'),
'tab_parent' => $form->getValue('tab_parent')
));
$this->flash('Tab inserted', 'admin/tab');
} catch (Exception $e) {
$this->flash($e->getMessage(), 'admin/tab');
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
if (isset($id) && $id != "") {
$values = $db->fetchRow($db->select()
->from(array('t'=>'tab'))
->joinLeft(array('tt'=>'tab_tag'), 'tt.tab_id = t.id'));
$form->populate($values);
}
$this->view->form = $form;
}
答案 0 :(得分:5)
要填充multiheckbox,您必须为数组提供必须检查的复选框的键:
$values = array(
'my_mylticheckbox' => array(1,3,5)
);
$form->populate($values);
或者如果您只想选中一个复选框:
$values = array('my_multicheckbox' => 3);
$form->populate($values);
因此,只需确保将数组作为multiheckbox的值传递。
答案 1 :(得分:0)
在将所有元素添加到$form>populate()
控件之前,您似乎在表单上执行了cat_parent
。颠倒这两个块的顺序 - 首先将猫添加到cat_parent
控件,然后添加获取db值并填充 - 应该这样做。
[作为旁注,我可能会将大部分内容推到表格本身。允许表单在构造函数中获取noChildsCategory
对象/数组,然后让init()
值添加multiOptions
。另外,我可能会将db读/写推送到某种模型对象中。然后控制器变得更加纤薄,您可以独立于控制器代码单元测试表单和模型。只是一个想法。]
答案 2 :(得分:0)
$values = $this->getEntityManager()
->getRepository('Application\Entity\Tab')
->findBy(array(
"id" => $id
));
$arrayIds = array();
foreach ($values as $value) {
$arrayIds[] = $value->getId();
}
$form->get('your_check_name')->setValue($arrayIds);
1)获取所有ID并将其直接设置到复选框表单字段中。它将自动填充。