多复选框 zend form 多重填充

发布于 2024-11-04 14:34:17 字数 5085 浏览 1 评论 0原文

我想用多个复选框填充表单,并且多个复选框分组在一起,因此如果我选中了多个复选框,我将查看它们全部。正常的填充不起作用。它只是让我选中其中一个复选框。这是我的代码。

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈场末日恋爱 2024-11-11 14:34:20
$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 并直接设置到复选框表单字段中。这样它将自动填充。

$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.

始终不够爱げ你 2024-11-11 14:34:19

看起来您在将所有元素添加到 cat_parent 控件之前正在表单上执行 $form>populate() 操作。颠倒这两个块的顺序 - 首先将猫添加到 cat_parent 控件中,然后添加获取数据库值并填充 - 应该可以解决问题。

[作为附带评论,我可能会将其中大部分内容放入表格本身。允许您的表单在构造函数中采用 noChildsCategory 对象/数组,然后让 init() 值添加 multiOptions。另外,我可能会将数据库读/写推送到某种模型对象中。然后控制器变得更加纤薄,您可以独立于控制器代码对表单和模型进行单元测试。只是一个想法。]

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.]

御弟哥哥 2024-11-11 14:34:18

要填充多复选框,您必须提供带有必须选中的复选框键的数组:

$values = array(
    'my_mylticheckbox' => array(1,3,5)
);
$form->populate($values);

或者如果您只想选中一个复选框:

$values = array('my_multicheckbox' => 3);
$form->populate($values);

因此,只需确保传递一个数组作为多复选框的值即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文