UPD:已解决-添加了额外的装饰器。
我需要得到以下结果:
<dd id="daily_722-element">
<input id="daily_722" type="checkbox" name="daily_722" value="1">
<span> some text </span>
</dd>
我需要"sometext"在复选框之后用html标记包装,但在/DD之前(而不是之后)!
我当前的代码如下:
$chk = new Zend_Form_Element_Checkbox('daily_shit');
$chk->setLabel('<span class="dt-note">'. $firstName. '</span>');
$chk->getDecorator('label')->setOption('escape', false);
所以它产生:
<dd id="daily_722-element">
<input id="daily_722" type="checkbox" name="daily_722" value="1">
</dd>
我不知道如何在复选框后注入额外的HTML。。。但在DD 内部
ZF1装饰器是一个已知的混淆源。不过,如果你付出一点努力,了解他们是如何构建HTML的,那么实现你想要的东西就很简单了。
我想您还没有覆盖ZF的表单元素的默认装饰器。然后它们是(记住它们是按顺序执行的,改变了以前的装饰器返回的内容):
- ViewHelper(渲染输入本身)
- 错误(如果需要,会附加错误消息)
- 描述(如果已设置,则附加元素描述)
- HtmlTag(用dd环绕)
- 标签(以dt包裹的标签为前缀)
现在您需要的是在输入(或错误/描述)之后添加<span> some text </span>
,但在它被dd
包装之前。这意味着应该在正确的位置将新的装饰器添加到现有装饰器链中。
$chk = new Zend_Form_Element_Checkbox('daily_shit');
$chk->setLabel('<span class="dt-note">firstName</span>');
$chk->getDecorator('label')->setOption('escape', false);
// Create a new decorator to render span you need
$postCheckboxDecorator = new Zend_Form_Decorator_Callback(
array(
'callback' => function () {
return '<span>some text</span>';
}
)
);
// Add it into existing chain of decorators, right after ViewHelper
$decorators = $chk->getDecorators();
$decorators = array_slice($decorators, 0, 1, true) +
array('PostCheckboxDecorator' => $postCheckboxDecorator) +
array_slice($decorators, 1, count($decorators) - 1, true);
// Replace element's decorators with a modified chain
$chk->setDecorators($decorators);
您可以为此编写自定义装饰器。方法render
接收原始内容并对其进行更改,然后返回更改后的内容。
class MyDecorator extends Zend_Form_Decorator_Abstract
{
public function render($content)
{
return $content . $this->_options['content'];
}
}
并将其用于表单构建
$form = new Zend_Form();
$chk = new Zend_Form_Element_Checkbox('daily_shit');
$chk->setLabel('<span class="dt-note">maxa</span>');
$chk->getDecorator('label')->setOption('escape', false);
$decorators = $chk->getDecorators();
$chk->clearDecorators();
$chk->addDecorator($decorators['Zend_Form_Decorator_ViewHelper']);
$chk->addDecorator(new MyDecorator(array('content' => '<span> some text </span>')));
$chk->addDecorator($decorators['Zend_Form_Decorator_Errors']);
$chk->addDecorator($decorators['Zend_Form_Decorator_Description']);
$chk->addDecorator($decorators['Zend_Form_Decorator_HtmlTag']);
$chk->addDecorator($decorators['Zend_Form_Decorator_Label']);
$form->addElement($chk);