根据您所需的示例输出,我不清楚您为什么要包含<dt>
on hidden 元素,特别是因为您没有应用任何标签,它们最终会成为不必要的标记。假设 label是不必要的,您可以通过以下方式达到预期的效果:
class TestForm extends Zend_Form
{
protected $_hiddenElementDecorator = array(
'ViewHelper',
array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')),
);
public function init()
{
$this->addElement('hidden', 'hiddenElement0');
$element = new Zend_Form_Element_Hidden('hiddenElement1');
$this->addElement($element);
}
public function loadDefaultDecorators()
{
foreach ($this->getElements() as $element) {
if ($element->getType() === "Zend_Form_Element_Hidden") {
$element->setDecorators($this->_hiddenElementDecorator);
}
}
parent::loadDefaultDecorators();
}
}
上述两个元素都将产生具有各自 id 的相同输出。例子:
<dd class="hidden">
<input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" />
</dd>
该方法中的foreach
循环在构建表单时loadDefaultDecorators()
迭代地应用于$this->_hiddenElementDecorator
每个隐藏的表单元素。如果你想在多个表单上应用隐藏元素装饰器,只需创建一个包含$_hiddenElementDecorator
变量和loadDefaultDecorators()
方法的父类。
但是,如果您一心想要包含<dt>
示例输出中描述的标签元素 ( ) 并应用“隐藏”类,那么您最终会得到:
<dt class="hidden"> </dt>
<dd class="hidden">
<input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" />
</dd>
...您将需要扩展Zend_Form_Decorator_Label
类并覆盖 render() 方法。看看if (null !== $tag)
块中的评论:
class My_Form_Decorator_Label extends Zend_Form_Decorator_Label
{
public function render($content)
{
$element = $this->getElement();
$view = $element->getView();
if (null === $view) {
return $content;
}
$label = $this->getLabel();
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$tag = $this->getTag();
$id = $this->getId();
$class = $this->getClass();
$options = $this->getOptions();
if (empty($label) && empty($tag)) {
return $content;
}
if (!empty($label)) {
$options['class'] = $class;
$label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
} else {
$label = ' ';
}
if (null !== $tag) {
require_once 'Zend/Form/Decorator/HtmlTag.php';
$decorator = new Zend_Form_Decorator_HtmlTag();
// Add 'class' => 'hidden' to the <dt> tag decorator options.
$decorator->setOptions(array('tag' => $tag, 'class' => 'hidden'));
$label = $decorator->render($label);
}
switch ($placement) {
case self::APPEND:
return $content . $separator . $label;
case self::PREPEND:
return $label . $separator . $content;
}
}
}
最后,要将新装饰器应用于所有隐藏的表单元素,您需要将元素前缀路径点添加到装饰器,并将标签装饰器重新添加$_hiddenElementDecorator
到类中的数组中TestForm
:
class TestForm extends Zend_Form
{
protected $_hiddenElementDecorator = array(
'ViewHelper',
array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')),
// Add back the label element.
array('Label', array('tag' => 'dt', 'class' => 'hidden')),
);
public function init()
{
$this->addElement('hidden', 'hiddenElement0');
$element = new Zend_Form_Element_Hidden('hiddenElement1');
$this->addElement($element);
}
public function loadDefaultDecorators()
{
foreach ($this->getElements() as $element) {
if ($element->getType() === "Zend_Form_Element_Hidden") {
$element->setDecorators($this->_hiddenElementDecorator);
}
}
// Add a decorator prefix path pointing to our new nifty decorator.
$this->addElementPrefixPath('My_Form_Decorator', '/path/to/Decorator', Zend_Form_Element::DECORATOR);
parent::loadDefaultDecorators();
}
}
像馅饼一样容易,不是吗?