[英]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.