在 Zend Form Select 中使用 HTML 实体
我正在填充一个 Select 表单元素,如果我尝试在值中使用 HTML 实体,它会被转换,而不是显示特殊字符。
此代码:
$form->field_name->addMultiOption('value', ' • label');
渲染:
<option value="one">&nbsp;&bull; label</option>
但我希望它是:
<option value="one"> • label</option>
如何在此处使用 HTML 实体?
提示?
我深入研究了代码,发现它在标签和值上使用了 Zend View Abstract 中的 escape()
函数。也许有人知道如何为特定表单元素覆盖/重载此函数?我不想默认情况下覆盖该行为。
Zend_View_Helper_FormSelect
类中的函数
protected function _build($value, $label, $selected, $disable)
{
if (is_bool($disable)) {
$disable = array();
}
$opt = '<option'
. ' value="' . $this->view->escape($value) . '"'
. ' label="' . $this->view->escape($label) . '"';
// selected?
if (in_array((string) $value, $selected)) {
$opt .= ' selected="selected"';
}
// disabled?
if (in_array($value, $disable)) {
$opt .= ' disabled="disabled"';
}
$opt .= '>' . $this->view->escape($label) . "</option>";
return $opt;
}
这是 Zend_View_Abstract
类中的函数:
private $_escape = 'htmlspecialchars';
/* SNIP */
public function escape($var)
{
if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities'))) {
return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_encoding);
}
return call_user_func($this->_escape, $var);
}
I am populating a Select form element, and if I try to use HTML Entities in the value, it get's converted, rather than displaying the special character.
This code:
$form->field_name->addMultiOption('value', ' • label');
Renders:
<option value="one"> • label</option>
But I want it to be:
<option value="one"> • label</option>
How do I use HTML entities here?
Hint?
I dug in the code and found that it's using the escape()
function from the Zend View Abstract on the label AND the value. Maybe someone knows how to override/overload this function for a specific form element? I don't want to override that behavior by default.
Function from the Zend_View_Helper_FormSelect
class
protected function _build($value, $label, $selected, $disable)
{
if (is_bool($disable)) {
$disable = array();
}
$opt = '<option'
. ' value="' . $this->view->escape($value) . '"'
. ' label="' . $this->view->escape($label) . '"';
// selected?
if (in_array((string) $value, $selected)) {
$opt .= ' selected="selected"';
}
// disabled?
if (in_array($value, $disable)) {
$opt .= ' disabled="disabled"';
}
$opt .= '>' . $this->view->escape($label) . "</option>";
return $opt;
}
This is the function from the Zend_View_Abstract
class:
private $_escape = 'htmlspecialchars';
/* SNIP */
public function escape($var)
{
if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities'))) {
return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_encoding);
}
return call_user_func($this->_escape, $var);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明这并不像我做的那么复杂。
我将其更改
为:
Turns out this isn't as complicated as I was making it.
I changed this:
To this:
您可以在填充特定字段时尝试关闭/清除 Zend 过滤器。
当您清除 Zend 过滤器时,您可以应用自己的、预先填充的过滤器。
You can try to switch off/clear Zend filters for specific fields when you populate them.
When you clear the Zend filters, you can apply your own, prior populating.