lee*_*eek 6 php zend-framework zend-form
我有一个像我这样创建的按钮元素:
$submit = new Zend_Form_Element_Button('submit');
$submit->setLabel('My Button');
$submit->setDecorators(array(
'ViewHelper',
array('HtmlTag', array('tag' => 'li'))
));
$submit->setAttrib('type', 'submit');
Run Code Online (Sandbox Code Playgroud)
这会生成以下HTML:
<li>
<label for="submit" class="optional">My Button</label>
<button name="submit" id="submit" type="submit">My Button</button>
</li>
Run Code Online (Sandbox Code Playgroud)
我想用<span>包裹按钮的内部,如下所示:
<button...><span>My Button</span></button>
Run Code Online (Sandbox Code Playgroud)
使用Zend_Form执行此操作的最佳方法是什么?
我已经尝试过,并且最终使用相同的方法无法实现这一目标.似乎最简单的方法是:
...
$submit->setLabel('<span>My Button</span>');
...
Run Code Online (Sandbox Code Playgroud)
但是,跨度将被转义.完全可以关闭标签装饰器的转义,但是,添加标签装饰器会导致输出错误,例如:
$decorator = array(
array('ViewHelper'),
array('HtmlTag', array('tag' => 'li')),
array('Label', array('escape' => false))
);
$submit = new Zend_Form_Element_Button('submit');
$submit->setLabel('<span>My Button</span>');
$submit->setDecorators($decorator);
$submit->setAttrib('type', 'submit');
Run Code Online (Sandbox Code Playgroud)
...渲染:
<label for="submit" class="optional"><span>My Button</span></label>
<li>
<button name="submit" id="submit" type="submit"><span>My Button</span></button>
</li>
Run Code Online (Sandbox Code Playgroud)
...除了在语义上不正确(易于修复)之外,还在逃避元素内的span标签.
所以你会怎么做?
我认为最好的方法(这是我对Zend_Form渲染的严格控制的元建议)是使用ViewScript装饰器.
$submit = new Zend_Form_Element_Button('submit');
$submit->setLabel('My Button');
$submit->setDecorators(array(array('ViewScript', array('viewScript' => '_submitButton.phtml'))));
$submit->setAttrib('type', 'submit');
Run Code Online (Sandbox Code Playgroud)
...然后在_submitButton.phtml中定义以下内容:
<li>
<?= $this->formLabel($this->element->getName(), $this->element->getLabel()); ?>
<button
<?php
$attribs = $this->element->getAttribs();
echo
' name="' . $this->escape($this->element->getName()) . '"' .
' id="' . $this->escape($this->element->getId()) . '"' .
' type="' . $this->escape($attribs['type']) . '"';
?>
<?php
$value = $this->element->getValue();
if(!empty($value))
{
echo ' value="' . $this->escape($this->element->getValue()) . '"';
}
?>
>
<span>
<?= $this->escape($this->element->getLabel()); ?>
</span>
</button>
</li>
Run Code Online (Sandbox Code Playgroud)
该_submitButton.phtml文件将需要在视图脚本目录(你可能是最好添加一个特定使用您的表单装饰$view->addScriptPath('/path/to/my/form/decorators')).
这应该呈现您正在寻找的东西.由于我在工作中遇到的灵活性问题,我才开始关注ViewScript装饰器.考虑到可以在元素对象上填充的所有成员,您会注意到我的脚本不是那么灵活,当然不在BNF中.也就是说,这是一个开始,它解决了你的问题.
小智 5
你可以这样做:
$this->addElement(new Zend_Form_Element_Button(
'send',
array(
'label' => '<span>registrieren</span>',
'class' => 'button-red',
'type' => 'submit',
'escape' => false,
'required' => false,
'ignore' => false,
)
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14904 次 |
| 最近记录: |