I'm building an ACL management form for the user roles in my application. I
saved the application structure (module - controller - action) in the
database. In another table I link the actions to the roles:

Module -< Controller -< Action -< Role_Action >- Role

In the code example below i'm looping all the modules, controllers and
actions. I add all the actions to a fieldbox (SubForm) grouped by module.
When i populate the checkboxes using the code below, nothing happened.

How can i populate my checkboxes using the database data?

#### Build the acion selectors ####################

// Fetch the modules
$modules = $moduleModel->fetchAll();

// Loop all the available modules
foreach ($modules as $module) {
    // Create a new subform based on the module
    $subFormActions = new Zend_Form_SubForm($module->module_alias);
    $subFormActions->setLegend($module->module_name);
    $subFormActions->setIsArray(false);

    // Fetch the controllers associated with the module
    $controllers = $module->findDependentRowset('ControllerModel');
    
    // Loop all the available controllers associated with the module
    foreach ($controllers as $controller) {
        // Fetch the actions associated with the controller
        $actions = $controller->findDependentRowset('ActionModel');

        // Loop all the available actions associated with the controller
        foreach ($actions as $action) {
            // Create a checkbox for the action
            $checkbox = new Zend_Form_Element_Checkbox($action->action_id); 
            $checkbox->setLabel($module->module_name . ' - ' .
$controller->controller_name . ' - ' . $action->action_name)
                     ->setValue($action->action_id)
                     ->setBelongsTo('actions')
                     ->setChecked(false);

            // Add the checkbox the the module subform
            $subFormActions->addElement($checkbox);
        }
    }
    
    // If the module subform contains elements, add it to the form
    if ($subFormActions->count() > 0) {
        $this->addSubForm($subFormActions, $module->module_alias);
    }
}



#### Form output ##################################

<input type="hidden" value="0" name="actions[1]"/>
<input id="actions-1" type="checkbox" value="1" name="actions[1]"/>

<input type="hidden" value="0" name="actions[3]"/>
<input id="actions-3" type="checkbox" value="1" name="actions[3]"/>

<input type="hidden" value="0" name="actions[3]"/>
<input id="actions-3" type="checkbox" value="1" name="actions[3]"/>

etc...



#### Populate the form data #######################

// Get the actions that are accessible for this role
$actions =
$roleActionModel->fetchAll($roleActionModel->select()->where('role_id = ?',
$item->role_id));
$actionArray = array();
$actionArray['actions'] = array();
foreach ($actions as $action) {
    $actionArray['actions'][$action->action_id] = '1';
}
$this->_form->populate($actionArray);

###################################################
-- 
View this message in context: 
http://www.nabble.com/Populate-checkboxes-%28array%29-in-Zend_Form-tp19305101p19305101.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to