如何在Zend Framework 3中注册自定义表单视图帮助器 [英] How to register custom form view helper in Zend Framework 3

查看:92
本文介绍了如何在Zend Framework 3中注册自定义表单视图帮助器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将继承的Zend Framework 2应用程序迁移到Zend Framework 3,并且在注册我的自定义表单视图助手时遇到了一些困难.当应用程序使用版本2时,助手将起作用,并且主要用于添加标签属性以实现可访问性.例如,这是一个自定义FormText.php帮助程序.

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';
    }
}

在我的应用程序的第2版中,使用以下方法(仅显示1个简短的帮助程序)将帮助程序注册到Module.php中(不确定为什么不在module.config.php'中注册):

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',

        ),
    );
}

在该应用程序的ZF3版本中,我试图在module.config.php的return语句中使用以下数组元素:

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,
    ],

],

尽管"cdn"帮助程序已正确注册并可以正常工作,但它不适用于表单视图帮助程序.表单视图助手不需要任何注入的依赖关系,因此我没有为其使用自定义工厂类.

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.

我确实在application.config.php中将"Zend/Form"列为模块,并且知道标准的Zend表单视图助手正在工作.

I do have 'Zend/Form' listed as a module in application.config.php and know that the standard Zend form view helpers are working.

我使用SO问题中的代码示例尝试了上述代码的许多变体来注册帮助器,尽管所有问题似乎都与普通视图帮助器有关,而不是与表单视图帮助器有关.

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.

谢谢.

推荐答案

从我工作的公司的一个活跃项目中举一个例子.我们还有一些默认的ZF3 Form ViewHelpers被我们自己覆盖,以与前端框架进行交互.主题名称是"Alpha"(我认为;-))

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 ;-) )

我们使用以下内容:

'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,
    ],
],

查看帮助程序本身:

// 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;
    }
}

因此,从本质上讲,我们不需要创建自己的ViewHelper,而无需更改Zend Framework 3提供的视图帮助.因为我们只是更新"现有的ViewHelper,所以不必创建新的工厂(没有其他要求).

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已使用可调用的名称注册了ViewHelpers(因此您可以执行$this->formRow(...)$this->formSelect(...).我们只是劫持了他们的配置,并用我们自己的类替换了所需的类." ,当我们有一个完全生成的表单(<?= $this->form($form) ?>)时,ZF会为我们完成所有工作.

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.

.phtml中的实现:

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


为使该配置更适合将来使用,我认为您现在可以用FQCN替换可调用的字符串(尚未对此进行测试)


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))

这篇关于如何在Zend Framework 3中注册自定义表单视图帮助器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆