Php 如何在Zend Framework 1中使用表单的自定义视图帮助器?
我尝试创建一个自定义ViewHelper来集成Dennis D的代码:Php 如何在Zend Framework 1中使用表单的自定义视图帮助器?,php,zend-framework,Php,Zend Framework,我尝试创建一个自定义ViewHelper来集成Dennis D的代码: label = '<label ' . $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">' . $opt_label .'</label>'; // Wrap the radios in labels $radio = '<input type="'
label = '<label ' . $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
. $opt_label
.'</label>';
// Wrap the radios in labels
$radio = '<input type="' . $this->_inputType . '"'
. ' name="' . $name . '"'
. ' id="' . $optId . '"'
. ' value="' . $this->view->escape($opt_value) . '"'
. $checked
. $disabled
. $this->_htmlAttribs($attribs)
. $endTag;
if ('prepend' == $labelPlacement) {
$radio = $label . $radio;
}
elseif ('append' == $labelPlacement) {
$radio .= $label;
}
}
但什么也没发生。我错在哪里?提前谢谢。我的自定义表单类型有点不同。我通常扩展类并将
$\u帮助器
设置为我创建的新视图帮助器,而不是向现有的Zend\u Form\u Element\u Radio
元素添加装饰器
所以对于你的班级来说可能是这样的:
class Application_Form_Element_NewRadios
extends Zend_Form_Element_Radio
{
$_helper = 'newRadios';
}
class Application_View_Helper_NewRadios
extends Zend_View_Helper_FormRadio
{
// .......
}
$this->addPrefixPath(
'Application_Form_Element',
'Library/Application/Form/Element',
'element'
);
问题的根源在于,视图助手的结尾与Zend助手的结尾相同,并且框架首先查看Zend助手。有两个选项,要么告诉表单首先查找视图帮助程序,要么给它一个新名称,该名称不会与Zend视图帮助程序冲突
因此,视图辅助对象将变成如下所示:
class Application_Form_Element_NewRadios
extends Zend_Form_Element_Radio
{
$_helper = 'newRadios';
}
class Application_View_Helper_NewRadios
extends Zend_View_Helper_FormRadio
{
// .......
}
$this->addPrefixPath(
'Application_Form_Element',
'Library/Application/Form/Element',
'element'
);
您可能需要告诉表单在何处查找新的表单元素类,因此在应用程序\u form\u Registration::init
方法中,您可能需要添加如下内容:
class Application_Form_Element_NewRadios
extends Zend_Form_Element_Radio
{
$_helper = 'newRadios';
}
class Application_View_Helper_NewRadios
extends Zend_View_Helper_FormRadio
{
// .......
}
$this->addPrefixPath(
'Application_Form_Element',
'Library/Application/Form/Element',
'element'
);
在application.ini
文件中,您可能还需要告诉应用程序在何处查找新的view helper类
resources.view.helperPath.Application_View_Helper = Library/Application/View/Helper
非常感谢。它工作得很好。是的,关于相同的结局,您是对的。总是很乐意帮助并找到仍在使用ZF1的人:-)