Reputation: 153
I want to populate the form with multicheckboxes and the multicheckboxes are grouped together, so if I have a multi check box checked, I will view them all. The normal populate is not working. It just gets me one of the checkboxes. This is my code.
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;
}
Upvotes: 1
Views: 3858
Reputation: 1
$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) Fetch all ids and direct set into checkbox form field.so it will automatically populate.
Upvotes: 0
Reputation: 4255
To populate multicheckbox you have to provide array with the key of checkboxes that has to be checked:
$values = array(
'my_mylticheckbox' => array(1,3,5)
);
$form->populate($values);
or if you want to make checked only one checkbox:
$values = array('my_multicheckbox' => 3);
$form->populate($values);
So, just make sure you pass an array as the value for you multicheckbox.
Upvotes: 5
Reputation: 14184
Looks like you are doing a $form>populate()
on the form before you add all the elements to the cat_parent
control. Reversing the order of those two blocks - first add the cats to the cat_parent
control, then add the get the db values and populate - should do the trick.
[As a side comment, I'd probably push much of this down into the form itself. Allow your form to take the noChildsCategory
object/array in the constructor, then let the init()
value add the multiOptions
. Also, I'd probably push the db read/write into a model object of some sort. Then the controller becomes much slimmer and you can unit test the forms and models independent of the controller code. Just a thought.]
Upvotes: 0