I am migrating an inherited Zend Framework 2 application to Zend Framework 3 and have run into a bit of difficulty registering my custom form view helpers. The helpers worked when the app was using version 2 and are mainly used for adding tag attributes for accessibility. For example this is a custom FormText.php helper.
<?php
namespace Application\Form\View\Helper;
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormInput;
class FormText extends FormInput
{
/**
* Attributes valid for the input tag type="text"
*
* @var array
*/
protected $validTagAttributes = array(
'name' => true,
'autocomplete' => true,
'autofocus' => true,
'dirname' => true,
'disabled' => true,
'form' => true,
'list' => true,
'maxlength' => true,
'pattern' => true,
'placeholder' => true,
'readonly' => true,
'required' => true,
'size' => true,
'type' => true,
'value' => true,
'aria-hidden' => true,
'aria-invalid' => true,
'aria-describedby' => true,
'aria-label' => true,
);
/**
* Determine input type to use
*
* @param ElementInterface $element
* @return string
*/
protected function getType(ElementInterface $element)
{
return 'text';
}
}
In version 2 of my application the helpers were registered in Module.php (not sure why not in module.config.php') using the following method (only showing 1 helper for brevity):
public function getViewHelperConfig()
{
return array(
'invokables' => array(
// Form helpers
'FormText' => 'Application\Form\View\Helper\FormText',
),
);
}
In the ZF3 version of the app I am trying to use the following array element in the return statement of module.config.php:
'view_helpers' => [
'factories' => [
View\Helper\Cdn::class => View\Helper\CdnFactory::class,
Form\View\Helper\FormText::class => InvokableFactory::class,
],
'aliases' => [
'cdn' => View\Helper\Cdn::class,
'FormText' => Form\View\Helper\FormText::class,
],
],
This does not work for the form view helper though the 'cdn' helper is being registered correctly and working as it should. The form view helper does not require any injected dependency so I am not using a custom factory class for it.
I do have 'Zend/Form' listed as a module in application.config.php and know that the standard Zend form view helpers are working.
I have unsuccessfully tried many variants of the code above to register the helper using examples of code from SO questions, though all the questions seem to relate to ordinary view helpers as opposed to form view helpers.
I would be very grateful for any suggestions on how I can get this working.
Thank you.
Module.phpthen just callFormTextusing$this->FormText()in your view. - Dolly Aswin