zend framework2 - Dynamically set SELECT attribute in zend2 -
what doing is, fetching list of companies database , passing form select element. created model file, returns array
//=== return array of $id => $name of companies use in dropdown in reports form public function gettotalresult($table, $type, $id) { $this->table = $table; $select = new select(); $spec = new where(); $spec->equalto('status', 1); if ($type == 'name') { $spec->equalto('id', $id); } $select->from($this->table); $select->where($spec); $resultset = $this->selectwith($select); //$resultset->buffer(); return $resultset; } public function resultlist($table){ $results = $this->gettotalresult($table, '', ''); foreach ($results $result) { $this->id[] = $result->id; $this->name[] = $result->name; } $result = array_combine($this->id, $this->name); return $result; } then tested in controller, returned wanted:
use zend\mvc\controller\abstractactioncontroller; use zend\view\model\viewmodel; use spangellogin\model\register; // <-- add import use spangellogin\model\companylist; // <-- add import class registercontroller extends abstractactioncontroller { protected $registertable; protected $companylist; public function getcompanylist() { if (!$this->companylist) { $sm = $this->getservicelocator(); $this->companylist = $sm->get('spangellogin\model\companylist'); } return $this->companylist; } public function indexaction() { //== list of companies $company_table = 'rs_company'; $sector_table = 'rs_sector'; $companieslist = $this->getcompanylist()->getname($company_table, 2); } } so want companieslist array passed in form's select element. how can achieve that. here form in using select.
use zend\form\form; use zend\form\element;
class sectorreportform extends form {
public function __construct($name = null) { // want ignore name passed parent::__construct('sectorreport'); $companiesarray = $this->companieslist(); $sectorsarray = $this->sectorslist(); $this->setattribute('method', 'post'); $this->setattribute('enctype','multipart/form-data'); $this->add(array( 'type' => 'zend\form\element\select', 'name' => 'company', 'attributes' => array( 'id' => 'company', 'multiple' => true, 'options' => $companiesarray, ), 'options' => array( 'label' => 'company', ), )); $this->add(array( 'name' => 'submit', 'attributes' => array( 'type' => 'submit', 'value' => 'upload', 'id' => 'submitbutton', 'class' => 'button violet right' ), )); } }
from design-perspective, best approach handle via dependency-injection. sneaky little buzzword confuses people much, nothing more forward data between objects :p
general dependency-injection forms can seen looking @ following answer, blog article
if not want go approach, can handle @ controller level, too.
$form = new my\form(); $select = $form->get('selectcountries'); $model = new my\countries(); $listdata = $model->getcountriesasarray(); $select->setvalueoptions($listdata); i still advise go different approach ;) keeps controllers more clean, too, thing. separation of concern!
Comments
Post a Comment