Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

autocomplete="off"


Go to End


4 Posts   1493 Views

Avatar
DeklinKelly

Community Member, 197 Posts

9 August 2011 at 4:22am

How can I add autocomplete="off" to a form field?

$fields->push( new TextField( "Your Phone") );

Avatar
martimiz

Forum Moderator, 1391 Posts

9 August 2011 at 4:58am

Edited: 09/08/2011 4:59am

Afaik the only way to do that is extending the (Text)Field. Something like

class MyAutocompleteTextField extends TextField { 

	function Field() {
		$attributes = array(
			'type' => 'text',
			'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''),
			'id' => $this->id(),
			'name' => $this->Name(),
			'value' => $this->Value(),
			'tabindex' => $this->getTabIndex(),
			'maxlength' => ($this->maxLength) ? $this->maxLength : null,
			'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null
		);

		if($this->disabled) $attributes['disabled'] = 'disabled';

		// add the new attribute here:              <==========
		$attributes['autocomplete'] = 'off';

		return $this->createTag('input', $attributes);
	}

}

Then

$fields->push( new MyAutocompleteTextField( "Your Phone") );

Avatar
Tama

Community Member, 138 Posts

23 February 2012 at 9:04am

On a similar topic, what's the best way to add autocomplete = "off" to the password field on the Login page?

Or all password fields for that matter.

I don't want to hack the core.

Avatar
swaiba

Forum Moderator, 1899 Posts

13 March 2012 at 1:19am

This is how I've done it, not the greatest but works...

_config.php

Object::useCustomClass('MemberLoginForm','CustomLoginForm');

CustomLoginForm.php

class CustomLoginForm extends MemberLoginForm {
	public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) {
		parent::__construct($controller, $name, $fields, $actions);
		Requirements::customScript(<<<JS
			jQuery(document).ready(function(){
				jQuery('#CustomLoginForm_LoginForm_Password').attr('autocomplete','off');
			});
JS
);
	}
}