How to create a checkbox tree filled from database using Zend_Form?
How do I do that with 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>
I've started with something like that:
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));
}
}
I've found the way: http://forums.zend.com/viewtopic.php?f=69&t=5171
$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:');
/// use where only if u want to put conditions on retrieving from db /// name of the table is : groups
精彩评论