[英]How to not escape html entities in a submit button value?
逃避我提交输入的价值感觉很奇怪。 当我尝试使用诸如<i class="fa fa-caret-right"></i>
之类的值时,它并没有转义引号,最终在我的输入元素中获得了随机属性。 因此,我已从输入字段切换到按钮。
$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,
],
],
]);
ZendButon 需要一个我不想要的标签。 所以我扩展了视图助手以从元素值 ( $buttonContent
) 中获取标签值。 因为没有label
属性,所以没有标签回显,但转义值仍然显示在按钮标签中。
表单按钮:
use Zend\Form\View\Helper\FormButton as ZendFormButton;
use Zend\Form\ElementInterface;
class FormButton extends ZendFormButton
{
/**
* Invoke helper as functor
*
* Proxies to {@link render()}.
*
* @param ElementInterface|null $element
* @param null|string $buttonContent
* @return string|FormButton
*/
public function __invoke(ElementInterface $element = null, $buttonContent = null)
{
if (!$element) {
return $this;
}
// New code here
if(is_null($buttonContent)) {
$buttonContent = $element->getValue();
}
return $this->render($element, $buttonContent);
}
}
输出:
<button type="submit" name="submit" value="Login <i class="fa fa-caret-right"></i>">Login <i class="fa fa-caret-right"></i></button>
如果我理解正确,你想显示登录▹
首先,您的结果表明您在代码中的某处有一个函数可以用它们的代码替换转义字符。 其次,在找到将&
替换为它的代码的那段代码后,您可能需要编写“ Login &#9657;
”才能获得“Login ▹” 在您的 HTML 代码中。
检查您的代码是否有用转义码替换特殊字符的函数。 如果不知道您的代码如何在浏览器中结束,轮到什么代码调用它等,就很难弄清楚。
但是,我 100% 确定您有一个函数可以获取值并用转义码替换特殊字符 - 我在这里找到了您的代码: http : //www.w3schools.com/charsets/ref_utf_geometric.asp
我不得不扩展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);
}
}
此代码将元素选项保存在受保护的变量中,以便可以在createAttributesString
函数中访问它。 在这个函数中,就在@todo
之前,我检查转义 html 选项是否存在,如果设置为true
则不转义属性值。
用途是:
$this->add([
'name' => 'submit',
'attributes' => [
'type' => 'submit',
'value' => 'Login ▹',
],
'options' => [
'disable_html_escape' => [
'value' => true,
],
],
]);
我正在使用一个数组,以便我可以特别选择哪个属性不转义 - 在这种情况下是value
。
渲染输出为:
<input type="submit" name="submit" value="Login ▸">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.