我不得不扩展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 ▸">