如何在Zend Framework 2中使用自定义表单视图助手? [英] How to use a custom form view helper in Zend Framework 2?

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

问题描述

我写了一个表单视图助手,它扩展了 Zend \ Form \ View \Helper\FormMultiCheckbox 并覆盖了它的 renderOptions(...) 方法:

 <?php 
命名空间MyNamespace \Form\ View\Helper;

使用Zend\Form\View\Helper\FormMultiCheckbox作为ZendFormMultiCheckbox;

类FormMultiCheckbox扩展ZendFormMultiCheckbox
{

保护函数renderOptions(...)
{
...
$ label = $ escapeHtmlHelper($ label);
$ labelOpen = $ labelHelper-> openTag($ labelAttributes);
switch($ labelPosition){
case self :: LABEL_PREPEND:
$ template = $ labelOpen。 $标签。 $ labelClose。 %s’的;
休息;
case self :: LABEL_APPEND:
默认值:
$ template ='%s'。 $ labelOpen。 $标签。 $ LABELCLOSE;
休息;
}
$ markup = sprintf($ template,$ input);

$ combinedMarkup [] = $标记;
...
}

}

下一步是注册新的视图助手。我正在这样做此处显示:

  namespace Application; 

使用Zend\Mvc\ModuleRouteListener;
使用Zend\Mvc\MvcEvent;
$ b $ class模块{

...

公共函数getViewHelperConfig(){
返回数组(
'invokables' =>数组(
'FormMultiCheckboxViewHelper'=>'MyNamespace \Form\View\Helper\FormMultiCheckbox',

);


$ / code $ / pre

现在我的问题是:如何让应用程序使用我的表单视图助手而不是 Zend \ Form \ View \Helper\FormMultiCheckbox

解决方案

虽然安德鲁斯的答案是有效的,但没有必要,只需使用默认的视图帮助器名称并将其映射到您的帮助器类,应用程序就可以使用您的帮助器

  public function getViewHelperConfig(){
return array($ b $'invokables'=> array(
'formmulticheckbox'=>' MyNamespace\Form\View\Helper\FormMultiCheckbox',
),
);
}


I wrote a form view helper, that extends the Zend\Form\View\Helper\FormMultiCheckbox and overwrites its renderOptions(...) method:

<?php
namespace MyNamespace\Form\View\Helper;

use Zend\Form\View\Helper\FormMultiCheckbox as ZendFormMultiCheckbox;

class FormMultiCheckbox extends ZendFormMultiCheckbox 
{

    protected function renderOptions(...)
    {
        ...
        $label     = $escapeHtmlHelper($label);
        $labelOpen = $labelHelper->openTag($labelAttributes);
        switch ($labelPosition) {
            case self::LABEL_PREPEND:
                $template  = $labelOpen . $label . $labelClose . '%s';
                break;
            case self::LABEL_APPEND:
            default:
                $template  = '%s' . $labelOpen . $label . $labelClose;
                break;
        }
        $markup = sprintf($template, $input);

        $combinedMarkup[] = $markup;
        ...
    }

}

The next step is to register the new view helper. I'm doing this like here shown:

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module {

    ...

    public function getViewHelperConfig() {
        return array(
            'invokables' => array(
                'FormMultiCheckboxViewHelper' => 'MyNamespace\Form\View\Helper\FormMultiCheckbox',
            )
        );
    }
}

Now my question: How can I make the application use my form view helper instead of Zend\Form\View\Helper\FormMultiCheckbox?

解决方案

Although Andrews answer works, it's not necessary, just use the default view helper name and map it to your helper class, the application will then use your helper instead

public function getViewHelperConfig() {
    return array(
        'invokables' => array(
            'formmulticheckbox' => 'MyNamespace\Form\View\Helper\FormMultiCheckbox',
        ),                
    );
}

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

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