Hi,
I have a ZF2 form and I need to populate the value_options for several
select elements with values from some tables.
I am creating the form by using the below code:
===============================================
$sm = $this->getServiceLocator();
$form = $sm->get('FormElementManager')->get('MyModule\Form\MyForm');
===============================================
The error I am getting is this:
=============================================================
File:
/pathtoproject/vendor/zendframework/zendframework/library/Zend/Form/FormElementManager.php:130
Message:
Plugin of type MyModule\Model\CategoryTable is invalid; must implement
Zend\Form\ElementInterface
=============================================================
The form is this:
=============================================================
class MyForm extends Form implements ServiceLocatorAwareInterface,
InitializableInterface {
protected $serviceLocator;
public function getServiceLocator() {
return $this->serviceLocator;
}
public function setServiceLocator(ServiceLocatorInterface
$serviceLocator) {
$this->serviceLocator = $serviceLocator;
}
protected function fields() {
}
public function init() {
$sm = $this->getServiceLocator();
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'category',
'options' => array(
'label' => 'category',
'empty_option' => 'select category',
'value_options' =>
$sm->get('MyModule\Model\CategoryTable')->getMenuOptions(),
),
));
....
}
=============================================================
The services are defined within Module.php of model:
=======================================================
'MyModule\Model\CategoryTable' => function($sm) {
$tableGateway = $sm->get('CategoryTableGateway');
$table = new CategoryTable($tableGateway);
return $table;
},
'CategoryTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Category());
return new TableGateway('categories', $dbAdapter,
null, $resultSetPrototype);
},
=======================================================
Any idea why using FormElementManager seems to make the ServiceLocator
from form to not have access to services I defined ?
I can overcome the error if instead of creating the form through
FormElementManager I am using this:
===========================
$sm = $this->getServiceLocator();
$form = new \MyModule\Form\MyForm;
$form->setServiceLocator($sm);
$form->init();
===========================
Thank you,
Cristian
PS I am almost newbie on ZF 2.x, I have been an intensive user of 1.x.
--
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com