[英]How I can use custom view helper for form in Zend Framework 1?
[英]How do I add a custom Form element helper to Zend\Form\View\Helper\FormElement's typemap?
我試圖將一個'DivElement'(只是一個空白div)作為練習添加到ZF2表格中。
我創建了Application\\Form\\Element\\DivElement
namespace Application\Form\Element;
use Zend\Form\Element;
class DivElement extends Element {
protected $attributes = array(
'type' => 'div'
);
}
為了配合它,我創建了Application\\Form\\View\\Helper\\DivElementHelper.php
namespace Application\Form\View\Helper;
use Zend\Form\ElementInterface;
use Zend\View\Helper\AbstractHelper;
class DivElementHelper extends AbstractHelper
{
public function __invoke(ElementInterface $element = null)
{
if (!$element)
return $this;
return $this->render($element);
}
/**
* @see \Zend\Form\View\Helper\AbstractHelper::render()
* @param ElementInterface $oElement
* @return string
*/
public function render(ElementInterface $oElement)
{
return "<div class='" . $oElement->getAttribute('class') . "'></div>";
}
}
我還在我的module.config.php中添加了一個invokable:
'view_helpers' => [
'invokables' => [
'formdiv' => DivElementHelper::class,
],
],
拼圖的最后一部分似乎是將我的formdiv幫助器放入Zend\\Form\\View\\Helper\\FormElement
的protected $typeMap
數組中。 我似乎無法確定哪個配置添加了以下映射:
'div'=>'formdiv'
對前者。 那里顯然有一個addType方法,所以有人想到了它! 我如何在那里學習我的課程?
謝謝。 亞歷克斯
ZF2已經確認目前沒有配置方式來實現這一目標。 看起來onBootstrap中的Module-way是唯一的路徑:
php $services->get('ViewHelperManager')->get('formelement')->addType( 'div', 'formdiv' );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.