我不确定我的回答是否会有所帮助,但请继续。
有一种方法可以用您自己的装饰器替换 ZF 装饰器,而无需编辑表单本身。
解决方案 #1:
here 描述的方法。或者简而言之:
假设你有一个表格:
class Form extends Zend_Form
{
function init ()
{
$this->addElement ('text', 'a', array (
'label' => 'Name'
));
}
}
然后在application.ini中有
resources.view.helperPath.Default_View_Helper = APPLICATION_PATH "/views/helpers"
添加一个新文件application/views/helpers/FormText.php
class Default_View_Helper_FormText extends Zend_Form_Decorator_Abstract
{
function formText ()
{
return 'It is I.';
}
}
就是这样。
解决方案 #2:
让我们有这个抽象类:
abstract class Application_Style
{
private $_object;
function __construct ($object = null)
{
if (isset ($object))
{
$this->apply ($object);
}
}
function apply ($object)
{
$this->setObject ($object);
if ($this->filter ())
{
$this->onApply ();
}
return $object;
}
function __call ($method, $arguments)
{
return call_user_func_array (array ($this->getObject (), $method), $arguments);
}
abstract protected function onApply ();
protected function filter ()
{
return true;
}
function setObject ($_object)
{
$this->_object = $_object;
}
function getObject ()
{
return $this->_object;
}
}
然后是后代。
class Application_Style_AdminForm extends Application_Style
{
function onApply ()
{
$this->addElement ($submit = new Zend_Form_Element_Submit ('submit', array(
'label' => 'Submit',
)));
$submit
->removeDecorator ('DtDdWrapper')
->addDecorator ('HtmlTag', array (
'placement' => Zend_Form_Decorator_HtmlTag::PREPEND,
'tag' => 'p',
'openOnly' => 1,
))
->addDecorator ('Custom', array ('text' => ' '))
;
}
}
在 onApply() 方法中可以是任何适合你的东西。例如,添加或删除装饰器。然后你可以像这样在你的表单上调用这个样式:
new Application_Style_AdminForm ($this);
它允许您操作表单表示,但无需直接更改它。