How can I enable formselect view helper in Zend Framework 2?

415 Views Asked by At

I am not using full ZF2 installation, only modules I have specified, including zendframework/zend-form

From formselect:

use Zend\Form\Element;

$element = new Element\Select('language');
$element->setValueOptions(array(
   '0' => 'French',
    //...
   '3' => 'Chinese'
));

echo $this->formSelect($element);

Problem:

when in non-view PHP:

PHP error: Call to undefined method formSelect()

when in *.phtml view file:

Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException'
with message 'Zend\View\HelperPluginManager::get was unable to fetch 
or create an instance for formselect'

Note:

$this->partial() works, as does $this->escapehtml(), but $this->formselect() does not

1

There are 1 best solutions below

0
On BEST ANSWER

Found a work-around - has to be done outside of view (phtml) file:

use Zend\Form\View\Helper\FormSelect;

$form = new FormSelect();
$selectHtml = $form->render($element);

//then
echo $selectHtml;

//or from view:
$this->partial($file, array('select' => $selectHtml));