如何使用Zend_Form创建从数据库填充的复选框树?

时间:2010-01-29 00:14:08

标签: zend-framework zend-form

如何使用Zend_Form做到这一点?

<ul>
<?php foreach ($this->roles as $module => $resources): ?>
    <li>
        <?php echo $module; ?>
        <ul>
            <?php foreach ($resources as $resource => $privileges): ?>
            <li>
                <?php echo $resource; ?>
                <ul>
                    <?php foreach ($privileges as $id => $privilege): ?>
                    <li><input type="checkbox" name="privileges[]" value="<?php echo $id ?>" /><?php echo $privilege; ?></li>
                    <?php endforeach; ?>
                </ul>
            </li>
            <?php endforeach; ?>
        </ul>
    </li>

<?php endforeach; ?>
</ul>

我从这样的事情开始:

class Admin_Form_AclRole extends Zend_Form {
    public function __construct($options = NULL)
    {
        parent::__construct($options);
        $this->setName('role');




        $role = new Zend_Form_Element_Text('role');
        $role->setLabel('Grupo:')
        ->addFilter('stripTags')
        ->addFilter('StringTrim')
        ->setAllowEmpty(false)
        ->setRequired(true);



        $privileges = new Zend_Form_Element_MultiCheckbox('foo', array(
        'multiOptions' => array(
            'foo' => 'Foo Option',
            'bar' => 'Bar Option',
            'baz' => 'Baz Option',
            'bat' => 'Bat Option'
            )
        )); 
        $privileges->addDecorator('HtmlTag', array('tag' => 'ul'));


        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Cadastrar');

        $this->addElements(array($role,$privileges,$submit));

    }
}

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

    $options = $db->fetchPairs(
    $db->select()->from('groups', array('id', 'caption'))
    ->where('id = ? ',$id)->order('caption ASC'), 'id');

    $groups = new Zend_Form_Element_MultiCheckbox('groups');

    $groups->addMultiOptions($options);

    $groups->setLabel('Choose all Groups:');

///仅在你想要从db检索条件时使用where ///表的名称是:groups