Reputation: 647
hi i am new in zend framework. i have created two modules names group and user. now i want the group dropdown in add user form so can you tell me how can i get this values ? the below i have mentioed the file structure
module
-user
-config
-src
-User
- Controller
- Form
- Model
-view
-user
- user
- index.phtml
- add.phtml
- edit.phtml
-group
-config
-src
-Group
- Controller
- Form
- Model
-view
-user
- user
- index.phtml
- add.phtml
- edit.phtml
i want the group dropdown list in add user form Thanks in advance
Upvotes: 0
Views: 125
Reputation: 1428
Here are an example.
I create a Role service to retrieve all roles available
public function toBasicArray($aI_roles = null){
if ( $aI_roles == null ){
$aI_roles = $this->getRoles();
}
foreach ($aI_roles as $role ){
$as_roles[$role->getId()] = $role->getName();
}
return $as_roles;
}
public function getAvailableUserRoles(){
$aI_roles = $this->I_roleRepository->getAvailableUserRoles();
return $this->toBasicArray($aI_roles);
}
Next I registred this service in Role Module.php
public function getServiceConfig() {
return array(
'factories' => array(
'Users\Service\RoleService' => 'Users\Service\RoleServiceFactory'
),
);
}
Now I can call this service from everywhere in my application. For exaple in my User controller I have a select options named "role" to set user role.
public function __construct($I_userService, $I_roleService, $I_userForm) {
$this->I_userService = $I_userService;
$this->I_roleService = $I_roleService;
$this->I_userForm = $I_userForm;
$this->I_userForm->get('role')->setValueOptions($this->I_roleService->getAvailableUserRoles());
}
In User form I set select options simply to:
$I_role = new Element\Select('role');
$I_role->setLabel('Ruolo');
$this->add($I_role);
Upvotes: 1