5

I am creating form inputs with the CakePHP Form helper and some inputs (Most of the time 'username' and 'password') are being autocompleted on create actions, login actions, etc.. this is annoying. I am guessing those are just more common so the browser is using its cookies to try to complete the inputs.

Anyways.. how do I disable this?

In my view:

...

echo $this->Form->input('username', array(
    'label' => 'Please enter your username',
    'class' => 'pure-u-1-2'
));
echo $this->Form->input('password', array(
    'label' => 'Please enter your password',
    'class' => 'pure-u-1-2'
));

...

What am I missing?

karns
  • 5,391
  • 8
  • 35
  • 57
  • Not marking as duplicate because you are asking for cakephp, but reading this question http://stackoverflow.com/questions/2530/how-do-you-disable-browser-autocomplete-on-web-form-field-input-tag and basic cake doc should have done it for you. – Nunser Sep 11 '14 at 14:38
  • Right, but I wanted a quick reference for CakePHP users in this situation. Back when I was beginning cake I struggled with this. – karns Sep 11 '14 at 14:46

2 Answers2

5

You can specify attributes to be sent to the form helper. Specify the attribute 'autocomplete' and set its value to 'off'.

...

echo $this->Form->input('username', array(
    'label' => 'Please enter your username',
    'class' => 'pure-u-1-2',
    'autocomplete' => 'off'
));
echo $this->Form->input('password', array(
    'label' => 'Please enter your password',
    'class' => 'pure-u-1-2',
    'autocomplete' => 'off'
));

...

Which results in something like this for your HTML:

<input name="data[Model][username]" autocomplete="off" class="pure-u-1-2" id="ModelUsername" type="text">

You may also do this on the whole form instead of just each input. Just specify the same attribute and value in the form create like so:

...

echo $this->Form->create('Model', array(
    'class' => 'class',
    'autocomplete' => 'off'
));

This will give you something like this in your HTML:

<form action=".../Model/Action" class="class" autocomplete="off" id="ModelActionForm" method="post" accept-charset="utf-8">

NOTE Several browsers will now ignore autocomplete="off" or autocomplete="false". The workaround is to place a hidden text and password field before all other inputs on your form. The browsers will fill those instead of the ones you want to leave alone.

karns
  • 5,391
  • 8
  • 35
  • 57
3

The best solution is to use autocomplete = new-password

It works great in Chrome and Firefox

Like this:

$this->Form->input('password', array('type' => 'password', 'autocomplete' => 'new-password'));
Mario Jaramillo
  • 567
  • 6
  • 9