magento后台表单支持autocomplete=off属性

出于安全考虑,公司后台的账户过段时间就会强制要求重新修改一次密码,公司很多同事修改密码后发现后台不能登录了,查了下用户信息发现不能登录的账号修改密码后用户名变成了邮箱,有的变成了手机号,导致用原来的用户名不能登录。

简单分析了一下,是浏览器自动填充的,为了杜绝后患,我打算把这里填用户信息的表单的autocomplete禁用掉

magento后台的form element,以text为例,只支持type, title, class, style, onclick, onchange, onkeyup, disabled, readonly, maxlength, tabindex这些属性的配置,这里新加一个autocomplete,配置后台表单的时候,加’autocomplete’ => ‘off’,如果没有此配置,默认是on的。

随便整的一段代码作为例子

1
2
3
4
5
6
7
8
$fieldset->addField('name', 'text', array(
'name' => 'tag_name',
'label' => Core::helper('tag')->__('Tag Name'),
'title' => Core::helper('tag')->__('Tag Name'),
'autocomplete' => 'off',
'required' => true,
'after_element_html' => ' ' . Core::helper('adminhtml')->__('[GLOBAL]'),
));

解释一下,magento后台的表单元素最终是通过$_element->getElementHtml()输出的

1
2
3
4
5
6
7
public function getElementHtml()
{
$html = '<input id="'.$this->getHtmlId().'" name="'.$this->getName()
.'" value="'.$this->getEscapedValue().'" '.$this->serialize($this->getHtmlAttributes()).'/>'."\n";
$html.= $this->getAfterElementHtml();
return $html;
}
1
2
3
4
public function getHtmlAttributes()
{
return array('type', 'title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'readonly', 'tabindex');
}

所以只需要给form element的getHtmlAttributes()方法添加autocomplete支持就可以了,getElementHtml()的时候会通过$this->serialize()生成表单元素内容。

我的问题是输入框自动填充造成的,所以里只我修改了Varien_Data_Form_Element_Text