Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable autocomplete in CakePHP form input box

Tags:

forms

cakephp

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?

like image 742
karns Avatar asked Sep 11 '14 14:09

karns


People also ask

How do I turn off autocomplete input?

Use the <input> tag with autocomplete attribute. Set the autocomplete attribute to value “off”.

How do I turn off autocomplete in textbox?

Click the Display tab. Do one of the following: To enable AutoComplete for the text box, select the Enable AutoComplete check box. To disable AutoComplete for the text box, clear the Enable AutoComplete check box.

How do I turn off autocomplete in Formik?

Easy solution: Instead of autocomplete='off', use autoComplete='off' and BANG!

How do you remove autofill in HTML?

Add autocomplete="off" onto <form> element; Add hidden <input> with autocomplete="false" as a first children element of the form.


2 Answers

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.

like image 148
karns Avatar answered Oct 07 '22 05:10

karns


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'));
like image 25
Mario Jaramillo Avatar answered Oct 07 '22 05:10

Mario Jaramillo