Zend framework2 如何不在提交按钮值中转义html实体?
在我的表单类中,我添加了一个提交按钮:Zend framework2 如何不在提交按钮值中转义html实体?,zend-framework2,zend-form-element,zend-form2,Zend Framework2,Zend Form Element,Zend Form2,在我的表单类中,我添加了一个提交按钮: $this->add([ 'name' => 'submit', 'attributes' => [ 'type' => 'submit', 'value' => 'Login ▹', ], ]); 输出为: <input name="submit" type="submit" value="L
$this->add([
'name' => 'submit',
'attributes' => [
'type' => 'submit',
'value' => 'Login ▹',
],
]);
输出为:
<input name="submit" type="submit" value="Login &#9657;">
<input type="submit" name="submit" value="Login ▸">
如何阻止值被转义
编辑
根据@RubenButurca答案,以下是输出:
如果我理解正确,您希望显示 登录▹ 首先,您的结果表明代码中的某个地方有一个函数,该函数用转义字符的代码替换转义字符。 其次,在您找到用其代码替换的代码片段后,您可能需要编写Login&38&35;9657; 为了获得登录&9657;在HTML代码中 检查代码中是否有用转义码替换特殊字符的函数。如果不知道您的代码在浏览器中是如何结束的,在轮到它时调用什么代码,等等,就很难弄清楚 但是,我100%确信您有一个函数,它接受值并用转义码替换特殊字符-我在这里找到了您的代码:我必须扩展FormSubmit视图帮助程序:
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormSubmit as ZendFormSubmit;
class FormSubmit extends ZendFormSubmit
{
protected $_options;
/**
* Render a form <input> element from the provided $element
*
* @param ElementInterface $element
* @throws Exception\DomainException
* @return string
*/
public function render(ElementInterface $element)
{
$this->_options = $element->getOptions();
return parent::render($element);
}
/**
* Create a string of all attribute/value pairs
*
* Escapes all attribute values
*
* @param array $attributes
* @return string
*/
public function createAttributesString(array $attributes)
{
$attributes = $this->prepareAttributes($attributes);
$escape = $this->getEscapeHtmlHelper();
$escapeAttr = $this->getEscapeHtmlAttrHelper();
$strings = [];
foreach ($attributes as $key => $value) {
$key = strtolower($key);
if (!$value && isset($this->booleanAttributes[$key])) {
// Skip boolean attributes that expect empty string as false value
if ('' === $this->booleanAttributes[$key]['off']) {
continue;
}
}
//check if attribute is translatable
if (isset($this->translatableAttributes[$key]) && !empty($value)) {
if (($translator = $this->getTranslator()) !== null) {
$value = $translator->translate($value, $this->getTranslatorTextDomain());
}
}
if(array_key_exists('disable_html_escape', $this->_options) &&
array_key_exists($key, $this->_options['disable_html_escape']) &&
$this->_options['disable_html_escape'][$key] === TRUE) {
$strings[] = sprintf('%s="%s"', $escape($key), $value);
continue;
}
//@TODO Escape event attributes like AbstractHtmlElement view helper does in htmlAttribs ??
$strings[] = sprintf('%s="%s"', $escape($key), $escapeAttr($value));
}
return implode(' ', $strings);
}
}
我使用一个数组,这样我可以选择,特别是,哪个属性不转义-在本例中是值
渲染输出为:
<input name="submit" type="submit" value="Login &#9657;">
<input type="submit" name="submit" value="Login ▸">
逃离提交输入的值感觉很奇怪。当我尝试像它这样的值时,没有转义引号,结果在我的输入元素中出现了随机属性。因此,我将输入字段切换为按钮
$this->add([
'name' => 'submit',
'type' => 'button',
'attributes' => [
'type' => 'submit',
'required' => 'required',
'value' => 'Login <i class="fa fa-caret-right"></i>',
],
'options' => [
'label_options' => [
'disable_html_escape' => true,
],
],
]);
输出:
<button type="submit" name="submit" value="Login <i class="fa fa-caret-right"></i>">Login <i class="fa fa-caret-right"></i></button>
数字减少是因为我输入了数字,而不是复制/粘贴代码。我已经更新了这个问题。你可能会发现这个答案作为一个解决办法->一个奇妙的解决方案,一个我以前见过的。问题是我很懒,只使用echo$this->form$this->myform;,与重复每个元素相反,标签和错误会被删除。使用button属性的问题是它需要一个标签,我不希望在submit按钮周围有标签。我已经创建了第二个视图帮助器,作为这个问题的最终解决方案。