How to register custom form view helper in Zend Framework 3

允我心安 提交于 2019-12-23 20:41:50

问题


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.


回答1:


Taking an example from an active project at the company I work at. We also have some default ZF3 Form ViewHelpers overwritten with our own to interface with the front-end framework. Theme name is "Alpha" (I think ;-) )

We use the following:

'view_helpers' => [
    // other stuff
    'invokables' => [
        'Zend\Form\View\Helper\FormCollection' => AlphaCollection::class,
        'Zend\Form\View\Helper\Form' => AlphaForm::class,
        'Zend\Form\View\Helper\FormRow' => AlphaRow::class,
        'Zend\Form\View\Helper\FormSelect' => AlphaSelect::class,
    ],
],

View helper itself:

// Namespace + use statements
class AlphaCollection extends FormCollection
{
    public function __construct()
    {
        parent::setWrapper('<div class="alpha-form-collection">%2$s%1$s%3$s</div>');
    }

    /**
     * @param \Zend\Form\ElementInterface $element
     * @param null $labelPosition
     * @return string
     */
    public function render(ElementInterface $element, $labelPosition = null)
    {
        $markup = parent::render($element, $labelPosition);

        $classes = 'input-field col s12 alpha-fieldset';

        if($element instanceof Collection)
        {
            $classes .= ' alpha-fieldset-collection';
        }

        $prepend = '<div class="' . $classes . '">';
        $append = '</div>';

        return $prepend . $markup . $append;
    }
}

So in essence, we're not so much creating our own ViewHelpers so much as changing the ones provided by Zend Framework 3. Because we're just "updating" existing ones, we don't have to create new Factories (no additional requirements).

Zend Framework has registered ViewHelpers with invokable names (so you can do $this->formRow(...) or $this->formSelect(...). We've just hijacked their configuration and replaced the class we need with our own. That way, when we have a form generated completely (<?= $this->form($form) ?>), ZF does all the work for us.

Implementation in a .phtml:

<!-- Internally uses the invokables we've modified, so this is all we need to do :) -->
<?= $this->form($form) ?>

To make the configuration a bit more future proof, I think you can replace the string invokable with a FQCN nowadays (have not tested this (yet))



来源:https://stackoverflow.com/questions/51787320/how-to-register-custom-form-view-helper-in-zend-framework-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!